DF中2列获得3列的条件

2024-06-14 21:37:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我想做一个IF条件来设置新列中的值('new\u col')。总体思路如下:

如果'Score'=np.nan&;''=2012年:返回1

elif'Score'==np.nan&;''=2013年:返回2

else:返回'Score'

data = {'year': [2010, 2011, 2012, 2013, 2014], 'Score': [10, 15, np.nan, np.nan, 3]}
df = pd.DataFrame(data, columns = ['year', 'Score'])



  year  Score
0  2010   10.0
1  2011   15.0
2  2012    1.0
3  2013    2.0
4  2014    3.0

Tags: dfnewdataifnpcolnan条件
2条回答

首先,对于测试,需要使用^{},然后可以通过^{}比较==,并通过^{}设置值:

m1 = df['Score'].isna() & df['year'].eq(2012)
m2 = df['Score'].isna() & df['year'].eq(2013)

df['Score'] = np.select([m1, m2], [1,2], default=df['Score'])
print (df)
   year  Score
0  2010   10.0
1  2011   15.0
2  2012    1.0
3  2013    2.0
4  2014    3.0

对于新列使用:

df['new_col'] = np.select([m1, m2], [1,2], default=df['Score'])
print (df)
   year  Score  new_col
0  2010   10.0     10.0
1  2011   15.0     15.0
2  2012    NaN      1.0
3  2013    NaN      2.0
4  2014    3.0      3.0

使用^{}^{}

condition_1 = (df['Score'].isnull()) & (df['year'] == 2012)
condition_2 = (df['Score'].isnull()) & (df['year'] == 2013)
values = [1, 2]

df['new_col'] = np.select([condition_1, condition_2], values, df['Score'])

np.select的语法是:numpy.select(condition_list, choice_list, default_value)

df

    year    Score   new_col
0   2010    10.0    10.0
1   2011    15.0    15.0
2   2012    NaN     1.0
3   2013    NaN     2.0
4   2014    3.0     3.0

相关问题 更多 >