简体   繁体   English

如何根据Pandas中具有特定值的列将多行合并为一行

[英]How to merge several rows into one row based on a column with specific value in Pandas

I have a DataFrame like this way: 我有这样的DataFrame:

item_id    revenue    month    year
1          10.0       01       2014
1          5.0        02       2013
1          6.0        04       2013
1          7.0        03       2013
2          2.0        01       2013
2          3.0        03       2013
3          5.0        04       2013

And I try to get the revenue of each item from January to March 2013 like following DataFrame: 我尝试从2013年1月到3月获得每个项目的收入,如下面的DataFrame:

item_it    revenue    year
1          12.0       2013
2          5.0        2013
3          0          2013

BUT, I am confused on how to implement it in Pandas. 但是,我对如何在熊猫中实现它感到困惑。 Any help would be appreciated. 任何帮助,将不胜感激。

You can slice first, then groupby and reindex to include 0 values. 您可以先切片, 然后 groupbyreindex以包含0值。

month_start, month_end = 1, 3
year = 2013

res = df.loc[df['month'].between(month_start, month_end) & df['year'].eq(year)]\
        .groupby('item_id')['revenue'].sum()\
        .reindex(df['item_id'].unique()).fillna(0)\
        .reset_index('revenue').assign(year=year)

print(res)

   item_id  revenue  year
0        1     12.0  2013
1        2      5.0  2013
2        3      0.0  2013

You can use groupby first then sum method to get the desire output. 你可以先使用groupby然后使用sum方法来获得所需的输出。

df.groupby(['year', 'item_id']).sum().reset_index().drop('month', axis=1).set_index('item_id')

         year  revenue
item_id               
1        2013     18.0
2        2013      5.0
3        2013      5.0
1        2014     10.0

暂无
暂无

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

相关问题 根据列值连接/合并一个熊猫数据框中的几行 - Concatenate/merge several rows within ONE pandas dataframe, based on column value 如何根据行中的特定值和熊猫中的另一列对行进行分组? - How to group rows based on specific value in a row and another column in pandas? 将多行按列值合并为一行,并根据连接行数拆分为多个数据帧,用于多列 - Combine several rows into one row by column value, and split into several dataframes based on number of concatenated rows, for several columns 将多行按列值连接成一行,并根据连接的行数将生成的 dataframe 拆分为多个数据帧 - Concatenate several rows into one row by column value, and split resulting dataframe into several dataframes based on number of concatinated rows 如何基于一行中的值和不同的列名合并两个熊猫数据帧? - How to merge two pandas dataframes based on a value in one row and with different column names? 如何根据 Pandas 数据框中的列值(int)合并行(带字符串)? - How to merge rows (with strings) based on column value (int) in Pandas dataframe? 熊猫:如何根据特定的行值将值应用于一组行? - Pandas: How to apply a value to a group of rows based on a specific row value? Pandas数据帧:如何根据标记的列值将一行转换为单独的行 - Pandas dataframe: how to turn one row into separate rows based on labelled column value 如何将两个特定行合并为一行 - How to Merge Two Specific Rows into One Row 基于第二列 pandas 合并一列中的行 - merge rows in one column based on second column pandas
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM