[英]Create new columns from unique row values in a pandas dataframe
I have a pandas dataframe like : 我有一个熊猫数据框,如:
yearPassed policyType count
0 1990 1 2000
1 1990 2 1400
2 1990 3 1200
3 1991 3 70
4 1992 2 1000
5 1992 3 800
I want to make a bar chart, color-coded by policyType column, and showing Year on X-Axis and count on Y-axis. 我想制作一个条形图,按policyType列进行颜色编码,并在X轴上显示Year,在Y轴上显示count。
I tried doing this: 我尝试这样做:
policy_vs_year.plot(x="yearPassed", y=["count", "policyType"], kind="bar")
plt.show()
but this gives a very bad plot. 但这给出了非常糟糕的情节。
So I decided to transform my dataframe into something like this (maybe it is easier to plot this way): 因此,我决定将我的数据框转换为如下所示(也许这样绘制更容易):
yearPassed 1 2 3
0 1990 2000 1400 1200
1 1991 0 0 70
2 1992 0 1000 800
My question is whether it is possible with elementary functions in pandas to achieve this ? 我的问题是,大熊猫的基本功能是否有可能实现这一目标? (or there are simpler alternatives to plot the dataframe in its original format - without having to reformat it ?)
(或者有更简单的选择以原始格式绘制数据框-无需重新格式化?)
This is easily done using df.pivot_table
: 使用
df.pivot_table
可以轻松完成此操作:
df = df.pivot_table(index=['yearPassed'],
columns=['policyType'], values='count').fillna(0)
df
policyType 1 2 3
yearPassed
1990 2000.0 1400.0 1200.0
1991 0.0 0.0 70.0
1992 0.0 1000.0 800.0
Furthermore, a stacked bar plot can be made using df.plot
: 此外,可以使用
df.plot
制作堆叠的条形图:
import matplotlib.pyplot as plt
df.plot(kind='bar', stacked=True)
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.