英文:
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")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论