简体   繁体   English

两个变量的并集以在Pandas Data Frame中形成一个新变量-Python

[英]Union of two variables to form a new one in Pandas Data Frame - Python

I have a data frame , and I want to create a new data frame based on the values of two columns. 我有一个数据框,我想基于两列的值创建一个新的数据框。 The pair of values alwyas would be: 'x' and 'x' or 'x' and NaN or NaN and 'x' or NaN and NaN . 该对值alwyas将是: 'x' and 'x''x' and NaNNaN and 'x'NaN and NaN So for the first three examples the values of the new varabiel would b 'x' and for the last one would be NaN . 因此,对于前三个示例,新varabiel的值将为b'x 'x' ,对于最后一个示例将为NaN Nan is missing value. Nan缺少价值。

The pandas data frame is: 熊猫数据框是:

Name    Company nameC.antiguo   Company nameC.completado
 ssd         X                           X
 adf         B                          NaN
 dsf         C                           C
 eee         NaN                        NaN
 wqe         NaN                        C

I tried the following code but it doen´t work at all. 我尝试了以下代码,但是根本不起作用。

pruebaofempat['bn']= pruebaofempat['Company nameC.antiguo'] + pruebaofempat['Company nameC.completado']

So, How Can I create the new variable correctly? 那么,如何正确创建新变量?

use .fillna : 使用.fillna

>>> df
  Name antiguo completado
0  ssd       X          X
1  adf       B        NaN
2  dsf       C          C
3  eee     NaN        NaN
4  wqe     NaN          C
>>> df['antiguo'].fillna(df['completado'])
0      X
1      B
2      C
3    NaN
4      C
Name: antiguo, dtype: object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM