在Pandas.plot或matplotlib.plot中使用两列数据帧作为一个轴

时间:2022-05-08 04:59:36

I am new to python and data analysis. I have a dataframe consisting of 3 columns - site (string object) , date (datetime) and value (integer). I want to plot a graph using the x axis as a concatenation of site and date and y axis as value. However I am not able to merge these 2 columns properly. It gives an error saying expecting a string or buffer but datetime.date found.

我是python和数据分析的新手。我有一个由3列组成的数据框 - site(字符串对象),date(datetime)和value(整数)。我想使用x轴绘制图形作为站点和日期以及y轴作为值的串联。但是,我无法正确合并这两列。它给出了一个错误,表示期望一个字符串或缓冲区,但找到了datetime.date。

df['index'] = df['site'] + df['date']

df ['index'] = df ['site'] + df ['date']

This is the line that I use. df is my dataframe.

这是我使用的线。 df是我的数据帧。

Any ideas would be very helpful.

任何想法都会非常有用。

1 个解决方案

#1


0  

(1) Give us a tiny dataset to make it easy to test solutions (like, six rows, with three x-axis-values

(1)给我们一个小数据集,以便于测试解决方案(例如,六行,具有三个x轴值

(2) For your creating-an-index-column approach, just convert the datetime object to a string, e.g.:

(2)对于create-an-index-column方法,只需将datetime对象转换为字符串,例如:

df['index'] =  ' '.join([df['site'], str(df['date'])]) 

(3) What kind of plot do you need? A bar plot, with that kind of categorical x-axis? Will there be only one value per grouped x-location?

(3)你需要什么样的情节?条形图,具有那种分类的x轴?每个分组的x位置只有一个值吗?

#1


0  

(1) Give us a tiny dataset to make it easy to test solutions (like, six rows, with three x-axis-values

(1)给我们一个小数据集,以便于测试解决方案(例如,六行,具有三个x轴值

(2) For your creating-an-index-column approach, just convert the datetime object to a string, e.g.:

(2)对于create-an-index-column方法,只需将datetime对象转换为字符串,例如:

df['index'] =  ' '.join([df['site'], str(df['date'])]) 

(3) What kind of plot do you need? A bar plot, with that kind of categorical x-axis? Will there be only one value per grouped x-location?

(3)你需要什么样的情节?条形图,具有那种分类的x轴?每个分组的x位置只有一个值吗?