[英]How do I combine two columns within a dataframe in Pandas?
Say I have two columns, A and B, in my dataframe: 假设我的数据框中有两列A和B:
A B
1 NaN
2 5
3 NaN
4 6
I want to get a new column, C, which fills in NaN cells in column B using values from column A: 我想得到一个新的列C,它使用A列中的值填充B列中的NaN单元格:
A B C
1 NaN 1
2 5 5
3 NaN 3
4 6 6
How do I do this? 我该怎么做呢?
I'm sure this is a very basic question, but as I am new to Pandas, any help will be appreciated! 我确信这是一个非常基本的问题,但由于我是Pandas的新手,任何帮助都将不胜感激!
You can use combine_first
: 你可以使用
combine_first
:
df['c'] = df['b'].combine_first(df['a'])
Docs: http://pandas.pydata.org/pandas-docs/version/0.17.0/generated/pandas.Series.combine_first.html 文档: http : //pandas.pydata.org/pandas-docs/version/0.17.0/generated/pandas.Series.combine_first.html
You can use where
which is a vectorized if/else: 您可以使用
where
这是一个量化的if / else:
df['C'] = df['A'].where(df['B'].isnull(), df['B'])
A B C
0 1 NaN 1
1 2 5 5
2 3 NaN 3
3 4 6 6
df['c'] = df['b'].fillna(df['a'])
那么.fillna将会做的是它将填充数据框中的所有Nan值我们可以传递任何值到这里我们传递值df ['a']所以这个方法将相应的'a'值放入'b'的南数值和最终答案将在'c'中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.