[英]selecting a column from pandas pivot table
I have the below pivot table which I created from a dataframe using the following code:我使用以下代码从数据帧创建了以下数据透视表:
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
As you can see from pivot table that it has 8 columns from 0-7 and now I want to plot some specific columns instead of all.正如您从数据透视表中看到的那样,它有 8 列,从 0 到 7,现在我想绘制一些特定的列而不是全部。 I could not manage to select columns.
我无法选择列。 Lets say I want to plot column 0 and column 2 against index.
假设我想针对索引绘制第 0 列和第 2 列。 what should I use for y to select column 0 and column 2?
我应该用什么来让 y 选择第 0 列和第 2 列?
plt.plot(x=table.index, y=??)
I tried with y = table.value['0', '2']
and y=table['0','2']
but nothing works.我试过
y = table.value['0', '2']
和y=table['0','2']
但没有任何效果。
You cannot select ndarray for y
if you need those two column values in a single plot you can use:如果您需要在可以使用的单个图中使用这两个列值,则不能为
y
选择 ndarray:
plt.plot(table['0'])
plt.plot(table['2'])
If column names are intergers then:如果列名是整数,则:
plt.plot(table[0])
plt.plot(table[2])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.