Skip to main content
 首页 » 编程设计

python之根据第 2 列的条件,使用第 1 列的输入创建新的 df 列

2024年12月31日8soundcode

我有一个包含 50 多列和重复 ID 的 DF。我感兴趣的部分看起来像这样

   ID      Value         year 
0   3       200          1995    
1   3       100          2001 
2   4       300          1995 
3   4       250          2000 

每个 ID = 1995 的所有第一个条目,但是第二个条目对应于一个 ValuedFrom 列(第二个条目是每个对象的退休年龄,因此在大多数情况下它的最后一个值)。我想合并所有这三列,这样我就可以得到两列,就像这样

     ID  Value1995   ValueRetired 
0   3       200           100    
1   4       300           250 

关于如何执行此操作的任何想法?

请您参考如下方法:

一般解决方案:

print (df) 
   ID  year  Value 
1   3  2003     95 
2   3  1995    200 
2   3  2001    100 
3   4  1995    300 
4   4  2000    250 
5   4  2004    150 
6   5  2000    201 
7   5  1995    202 <- remove this row with 1995, because last value of group 5, if seelct next row it is in another group 
8   6  2000    203 
9   6  2000    204 

首先选择 1995 和所有后续行的索引:

idx = df.index[(df['year'] == 1995) & (df.groupby('ID').cumcount(ascending=False) != 0)] 
idx2 = df.index.intersection(idx + 1).union(idx) 
df = df.loc[idx2] 
print (df) 
   ID  year  Value  ValuedFrom 
2   3  1995    200        1995 
2   3  2001    100        2001 
3   4  1995    300        1995 
4   4  2000    250        2000 

详细信息:

print (df.groupby('ID').cumcount(ascending=False)) 
1    2 
2    1 
2    0 
3    2 
4    1 
5    0 
6    1 
7    0 
8    1 
9    0 
dtype: int64 

然后更改 year 列的值以按 unstack reshape :

df['year'] = np.where(df['year'] == 1995, 'Value1995', 'ValueRetired') 
 
df = df.set_index(['ID', 'year'])['Value'].unstack().reset_index().rename_axis(None, axis=1) 
print (df) 
   ID  Value1995  ValueRetired 
0   3        200           100 
1   4        300           250