基于现有列值的大熊猫新列计算

时间:2015-12-08 03:24:38

标签: python pandas

source table

请参阅上面的源表:

我很有兴趣计算新专栏" Group"根据感兴趣的地段清单:

List of interest

专栏的价值"组"基于条件,如果在源表中的批次"批次"存在很多兴趣。如果不是这样,那么" Group"列中的值将从" LOT_VIRTUAL_LINE"细胞

期望的输出:

enter image description here

2 个答案:

答案 0 :(得分:1)

假设我们有一个名为lots_of_interest的{​​{1}},例如read_csv(path).loc[:, 'lots_of_interest'].tolist()的结果:

df['Group'] = df.apply(lambda x: x['LOT'] if x['LOT'].isin(lots_of_interest) else x['LOT_VIRTUAL_LINE'], axis=1)

答案 1 :(得分:1)

因为这个问题被标记为Pandas,我假设我们正在讨论数据帧和系列而不是普通列表。您可以使用loc找到符合条件的行和列(例如,LOTisin中的每个元素是否为lots of interest系列。

df = pd.DataFrame({'LOT': ['A1111', 'A2222', 'A3333', 'B1111', 'B2222', 'B3333'], 
                   'LOT_VIRTUAL_LINE': ['AAA'] * 3 + ['BBB'] * 3})
s = pd.Series(['A1111', 'B2222'], name='Lots Of Interest')
# or... df2 = pd.read_csv('file_path/file_name.csv')

# Value of 'GROUP' defaults to 'LOT_VIRTUAL_LINE'.
df['GROUP'] = df.LOT_VIRTUAL_LINE

# But gets overwritten by 'LOT' if it is in the 'Lots of Interest' series.
mask = df.LOT.isin(s)
# or... mask = df.LOT.isin(df2['Lots of Interest'])  # Whatever the column name is.
df.loc[mask, 'GROUP'] = df.loc[mask, 'LOT']

# Confirm results.
>>> df
     LOT LOT_VIRTUAL_LINE  GROUP
0  A1111              AAA  A1111
1  A2222              AAA    AAA
2  A3333              AAA    AAA
3  B1111              BBB    BBB
4  B2222              BBB  B2222
5  B3333              BBB    BBB
相关问题