我想做一个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
首先,对于测试,需要使用^{} ,然后可以通过^{} 比较} 设置值:
==
,并通过^{对于新列使用:
使用^{} 和^{} :
np.select
的语法是:numpy.select(condition_list, choice_list, default_value)
相关问题 更多 >
编程相关推荐