保存由Python Pandas生成的图表,文件名与CSV文件相同。

huangapple go评论71阅读模式
英文:

Save plot generated from python pandas with same name as of csv file

问题

我已经使用pandas从一个数据框中生成了一个图表我想将其保存到文件中

import pandas as pd
df=pd.read_csv("C:/Users/xyz/Desktop/carsdataset.csv")

X=df['columnname'].value_counts(normalize=True)
X.plot.barh()

这给我了我想要的图形。但我想要将这个图形保存在与相同文件名(即'carsdataset')相同位置,以便我可以对我选择的任何其他csv文件运行此代码,并且它应该自动使用相同名称(即csv文件的名称)保存图形。

英文:

I have used pandas to generate a plot from a df which I want to save to a file

import pandas as pd
df=pd.read_csv("C:/Users/xyz/Desktop/carsdataset.csv")

X=df.'columnname'.value_counts.(normalize=True)
X.plot.barh() 

This gives me the figure(image) that I wanted. But I want to save this figure(image) at the same location with same filename(i.e 'carsdataset') so that I can run this code for any other csv file that I pick and it should automatically save the figure(image) with the same name(i.e name of the csv file)

答案1

得分: 1

扩展Redox所写的内容。
这样能解决您的问题吗?

import pandas as pd

dset_path = "C:/Users/xyz/Desktop/carsdataset.csv"
df = pd.read_csv(dset_path)

X = df['columnname'].value_counts(normalize=True)
X.plot.barh().get_figure().savefig(dset_path[:-3] + "png")
英文:

Expanding on what Redox wrote.
Would this solve your problem?

import pandas as pd

dset_path = "C:/Users/xyz/Desktop/carsdataset.csv"
df=pd.read_csv(dset_path)

X=df.'columnname'.value_counts.(normalize=True)
X.plot.barh().get_figure().savefig(dset_path[:-3]+"png")

huangapple
  • 本文由 发表于 2023年3月15日 18:50:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75743677.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定