SUM特定的列值,在行满足条件并放入新行的位置具有整数



我希望对前两个列值求和,其中row=="BB"处有数字,并放在下方的新行中

数据

ID  Q121 Q221 Q321 Q421
AA  8.0  4.8  3.1  5.3
BB  0.6  0.7  0.3  0.9


所需

ID  Q121 Q221 Q321 Q421
AA  8.0  4.8  3.1  5.3
BB  0.6  0.7  0.3  0.9
NEW 1.3  0.0  1.2  0.0

执行

mask = df['ID'].eq('BB')
df[NEW] = df.iloc[2,1:2].sum()

我取第2行、第1列和第2列的总和

欢迎提出任何建议。

您可以在DF之外创建一个列表,然后将其添加到DataFrame

df = pd.DataFrame([['AA', 8.0, 4.8, 3.1, 5.3], ['BB', 0.6, 0.7, 0.3, 0.9]], columns=['ID', 'Q121', 'Q221', 'Q321', 'Q421']).set_index('ID')
new = []
for z in [x+y for x,y in zip(df.loc['BB'][0:-1:2],df.loc['BB'][1::2])]:
new.extend([z,0])
df.loc['New'] = new

您可以使用滚动窗口来计算两列对的总和,然后连接结果

col=[1,0,1,0]
df2=pd.concat([df, 
df.loc[df['ID'].isin(['AA','BB']) ]
.rolling(2, axis=1).sum() # calc rolling windows sum of two columns
.shift(-1, axis=1) # shift result to left
.fillna(0) # fill null values
.mul(col) # multipley to keep sum in alternate columns
]
)
df2.sort_index(axis=0, inplace=True)

#create a column with a new ID
df2['ID2']=df2['ID']+'_New'
# fill NA values with the row above
df2['ID2'].fillna(method='ffill', inplace=True)
#reset index
df2=df2.reset_index()
# where the ID is null (our new summed row), assign the ID2 to ID
df2.loc[df2['ID'].isna(), 'ID'] =   df2['ID2']
#drop unwanted columns
df2.drop(columns=['ID2', 'index'], inplace=True)
df2
ID      Q121    Q221    Q321    Q421
0   AA       8.0    4.8     3.1     5.3
1   AA_New  12.8    0.0     8.4     0.0
2   BB       0.6    0.7     0.3     0.9
3   BB_New  1.3     0.0     1.2     0.0```

最新更新