Python 从数据透视表中选择列
我使用以下代码从数据帧创建了以下透视表:Python 从数据透视表中选择列,python,pandas,pivot-table,python-3.5,Python,Pandas,Pivot Table,Python 3.5,我使用以下代码从数据帧创建了以下透视表: table = pd.pivot_table(df, values='count', index=['days'],columns['movements'], aggfunc=np.sum) movements 0 1 2 3 4 5 6 7 days 0 2777 51 2 1 6279 200 7 3 2
table = pd.pivot_table(df, values='count', index=['days'],columns['movements'], aggfunc=np.sum)
movements 0 1 2 3 4 5 6 7
days
0 2777 51 2
1 6279 200 7 3
2 5609 110 32 4
3 4109 118 101 8 3
4 3034 129 109 6 2 2
5 2288 131 131 9 2 1
6 1918 139 109 13 1 1
7 1442 109 153 13 10 1
8 1085 76 111 13 7 1
9 845 81 86 8 8
10 646 70 83 1 2 1 1
正如您从pivot表中看到的,它从0到7有8列,现在我想绘制一些特定列,而不是所有列。我无法选择列。假设我想根据索引绘制第0列和第2列。y应该使用什么来选择列0和列2
plt.plot(x=table.index, y=??)
我试过使用
y=table.value['0','2']
和y=table['0','2']
但没有任何效果。您不能为y
选择ndarray,如果您需要在单个绘图中使用这两个列值,您可以使用:
plt.plot(table['0'])
plt.plot(table['2'])
如果列名是整数,则:
plt.plot(table[0])
plt.plot(table[2])
谢谢列名是整数。当我进行plt.plot(x=table.index,y=table[0])时,我并没有得到任何错误,但不知何故,plot函数出现了一些错误。它只显示一个x和y范围为-0.5-0.5的空绘图。但是,执行打印(表[0])确实显示了正确的数据。不知何故,plt.plot(表[0])和后来的plt.plot(表[2])似乎是可行的。