簡體   English   中英

熊貓分組並減去行

[英]Pandas groupby and subtract rows

我有以下數據框:

id variable year value
1      a    2020   2
1      a    2021   3
1      a    2022   5
1      b    2020   3
1      b    2021   8
1      b    2022   10

我想對 id 和變量進行分組,並從組的所有行中減去 2020 值。 所以我會得到:

id variable year value
1      a    2020   0
1      a    2021   1
1      a    2022   3
1      b    2020   0
1      b    2021   5
1      b    2022   7

我怎樣才能做到這一點?

如果不確定2020是否是每個組的第一個,請使用DataFrame.merge

df1 = df[df['year'].eq(2020)]
df['value'] -= df.merge(df1,how='left',on=['id','variable'],suffixes=('_',''))['value'].values
print (df)
   id variable  year  value
0   1        a  2020      0
1   1        a  2021      1
2   1        a  2022      3
3   1        b  2020      0
4   1        b  2021      5
5   1        b  2022      7

如果2020始終是每個組的第一個,請使用GroupBy.transformGroupBy.first

df['value'] -= df.groupby(['id','variable'])['value'].transform('first')
print (df)
   id variable  year  value
0   1        a  2020      0
1   1        a  2021      1
2   1        a  2022      3
3   1        b  2020      0
4   1        b  2021      5
5   1        b  2022      7

編輯:

如果數據重復,每組2020行解決方案首先刪除重復項並僅減去第一個值:

print (df)
   id variable  year  value
0   1        a  2020      3
1   1        a  2020      2
2   1        a  2022      5
3   1        b  2020      3
4   1        b  2021      8
5   1        b  2022     10

df1 = df[df['year'].eq(2020)]
df['value'] -= df.merge(df1.drop_duplicates(['id','variable']),
                        how='left',
                        on=['id','variable'],
                        suffixes=('_',''))['value'].values

print (df)
   id variable  year  value
0   1        a  2020      0
1   1        a  2020     -1
2   1        a  2022      2
3   1        b  2020      0
4   1        b  2021      5
5   1        b  2022      7

或聚合值,例如通過重復數據刪除的sum

print (df)
   id variable  year  value
0   1        a  2020      3
1   1        a  2020      1
2   1        a  2022      5
3   1        b  2020      3
4   1        b  2021      8
5   1        b  2022     10

df = df.groupby(['id','variable','year'], as_index=False).sum()
print (df)
   id variable  year  value
0   1        a  2020      4
1   1        a  2022      5
2   1        b  2020      3
3   1        b  2021      8
4   1        b  2022     10

df1 = df[df['year'].eq(2020)]
df['value'] -= df.merge(df1, how='left',
                        on=['id','variable'],
                        suffixes=('_',''))['value'].values

print (df)
   id variable  year  value
0   1        a  2020      0
1   1        a  2022      1
2   1        b  2020      0
3   1        b  2021      5
4   1        b  2022      7

盡管2020不是我們可以使用的第一個組GroupBy.transformSeries.where

df['value']= df['value'].sub(df['value'].where(df['year'].eq(2020))
                                        .groupby([df['id'],df['variable']])
                                        .transform('max'))
print(df)
   id variable  year  value
0   1        a  2020    0.0
1   1        a  2021    1.0
2   1        a  2022    3.0
3   1        b  2020    0.0
4   1        b  2021    5.0
5   1        b  2022    7.0

如果年份string您可能需要

df['year'].eq('2020')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM