简体   繁体   English

从 pandas dataframe 中的行创建 X 轴

[英]Create X Axis from row in pandas dataframe

I'm trying to plot the x-axis from the top row of my dataframe, and the y-axis from another row in my dataframe.我正在尝试 plot 从我的 dataframe 的第一行的 x 轴,以及从我的 dataframe 的另一行的 y 轴。 My dataframe looks like this:我的 dataframe 看起来像这样:

sector_data =扇区数据=

Time    13:00   13:15   13:30   13:45
Utilities   1235654 1456267 1354894 1423124
Transports  506245  554862  534685  524962
Telecomms   142653  153264  162357  154698

I've tried a lot of different things, with this seeming to make the most sense.我尝试了很多不同的东西,这似乎是最有意义的。 But nothing works:但没有任何效果:

sector_data.plot(kind='line',x='Time',y='Utilities')
plt.show()

I keep getting: KeyError: 'Time'我不断收到:KeyError:'时间'

It should end up looking like this:它最终应该看起来像这样:

Expected Chart预期图表

enter image description here在此处输入图像描述

Given the little information you provide I believe this should help:鉴于您提供的信息很少,我相信这应该会有所帮助:

df = sector_data.T
df.plot(kind='line',x='Time',y='Utilities')
plt.show()

This is how I made a case example (I have already transposed the dataframe)这就是我制作案例的方式(我已经转置了数据框)

import pandas as pd
import matplotlib.pyplot as plt
a = {'Time':['13:00','13:15','13:30','13:45'],'Utilities':[1235654,1456267,1354894,1423124],'Transports':[506245,554862,534685,524962],'Telecomms':[142653,153264,162357,154698]}
df = pd.DataFrame(a)
df.plot(kind='line',x='Time',y='Utilities')
plt.show()

Output: Output: 在此处输入图像描述

Let's take an example DataFrame:我们以DataFrame为例:

import pandas as pd
 df = pd.DataFrame({'ColA':['Time','Utilities','Transports','Telecomms'],'ColB':['13:00', 1235654, 506245, 142653],'ColC':['14:00', 1234654, 506145, 142650], 'ColD':['15:00', 4235654, 906245, 142053],'ColE':['16:00', 4205654, 906845, 742053]})
df = df.set_index('ColA') #set index for the column A or the values you want to plot for

Now you can easily plot with matplotlib现在您可以轻松地使用 plot 和 matplotlib

plt.plot(df.loc['Time'].values,df.loc['Utilities'].values)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM