在Python中进行条件数据归纳

问题描述 投票:0回答:1

我正试图有条件地推算我的数据集中的值。

比如我有三列,如果列1是1,那么列2是0,列3是0;如果列1是2,那么列2是Mean(),列3是Mean()。

我试着用函数any()运行if语句,并分别定义了条件。

然而条件并没有根据条件得到满足,我要么得到所有的均值,要么得到所有的零。

具体代码如下。

if (df['Retention_Term'] == 6):
    df.cl_tot_calls_term_seq_1.replace(999, np.nan,inplace = True)
df['cl_tot_calls_term_seq_1'].fillna(df['cl_tot_calls_term_seq_1'].median(),inplace= True)
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
python-3.x pandas valueerror
1个回答
0
投票

这样试一试。

mask1 = df['Retention_Term']==6
mask2 = df['cl_tot_calls_term_seq_1'] == 999 
df.loc[mask1 & mask2, 'cl_tot_calls_term_seq_1'] = np.nan

那么其他的应该就可以了。

df['cl_tot_calls_term_seq_1'].fillna(df['cl_tot_calls_term_seq_1'].median(), inplace= True)
© www.soinside.com 2019 - 2024. All rights reserved.