这是一些说明我的问题的代码
import pandas as pd
import numpy as np
# Create random dataframe
df = pd.DataFrame({'col1': np.random.randint(0, 9, 10),
'col2': np.random.randint(0, 9, 10),
'col3': np.random.randint(0, 9, 10)})
# Can this be written in a better way?
df.loc[df['col1'] > df['col2'], 'col3'] = (df.loc[df['col1'] > df['col2'], 'col2'] +
df.loc[df['col1'] > df['col2'], 'col3'])
基本上,在某些情况下,我想对具有较长条件的数据框进行子集化,并用基于其他列的表达式替换值。这就是我最终写它的方式,但我认为可能有更好的方法来做到这一点
慕姐4208626
宝慕林4294392
相关分类