我有以下DataFrame:
df = pd.DataFrame([[1,2,3],
[-1,3,2],
[3,4,5],
[-3,4,5]], columns=['A','B','C'])
condition = df['A']>0
df.loc[condition, 'B'] = df['B'] + 1
df.loc[condition, 'C'] = df['C'] * 2
print(df.head())
我基于相同的条件对两个列执行两个不同的操作。将这两行代码组合成一行代码的最佳方法是什么?
df.loc[condition, 'B'] = df['B'] + 1
df.loc[condition, 'C'] = df['C'] * 2
两个Series
使用concat
,因为不同的操作:
df.loc[condition, ['B','C']] = pd.concat([df['B'] + 1, df['C'] * 2], axis=1)
print(df)
A B C
0 1 3 6
1 -1 3 2
2 3 5 10
3 -3 4 5
在我看来2行更可读:
df.loc[condition, 'B'] += 1
df.loc[condition, 'C'] *= 2
一个选项是assign:
df.assign(B = df.B.mask(condition, df.B + 1),
C = df.C.mask(condition, df.C * 2))
Out[298]:
A B C
0 1 3 6
1 -1 3 2
2 3 5 10
3 -3 4 5