简体   繁体   English

从熊猫数据框中的唯一行值创建新列

[英]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()

在此处输入图片说明

Just using pandas 只用pandas

df.set_index(['yearPassed','policyType']).unstack(-1).fillna(0).plot.bar(stacked=True)

在此处输入图片说明

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

相关问题 Pandas dataframe 使用基于上述行的值创建新列 - Pandas dataframe create new columns with values based on above row 使用 pandas 中的列及其唯一值创建一个 dataframe - Create a dataframe with columns and their unique values in pandas Pandas:从唯一的行值对中创建带有元组作为标签的列 - Pandas: create columns with tuples as labels from unique pairs of row values 如果两列的值不同,则在 dataframe 中创建新行 - Create new row in a dataframe if values from two columns are different 根据 pandas 中的行值创建新列 - Create new columns according row values in pandas 删除列并为每个删除的列创建唯一的行 Pandas Dataframe - Remove Columns And Create Unique Row For Each Removed Column Pandas Dataframe 获取多列的唯一值作为 Pandas 中的新数据框 - Get unique values of multiple columns as a new dataframe in pandas 根据唯一值创建 pandas DataFrame 的新列? - Creating new columns of pandas DataFrame based on unique values? Python-如何从现有列中的唯一值和相应值创建数据框中的新列? - Python - how to create new columns in a dataframe from the unique values from an existing column with corresponding values? 如何从熊猫数据框创建汇总新行并将其添加回仅特定列的同一数据框 - How to create a summarize new row from a pandas Dataframe and add it back to the same Dataframe for only specific columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM