将熊猫DataFrame中的列值与“NaN”值连接起来

时间:2022-05-21 20:22:50

I'm trying to concatenate Pandas DataFrame columns with NaN values.

我正在尝试将熊猫DataFrame列与NaN值连接起来。

In [96]:df = pd.DataFrame({'col1' : ["1","1","2","2","3","3"],
                'col2'  : ["p1","p2","p1",np.nan,"p2",np.nan], 'col3' : ["A","B","C","D","E","F"]})

In [97]: df
Out[97]: 
  col1 col2 col3
0    1   p1    A
1    1   p2    B
2    2   p1    C
3    2  NaN    D
4    3   p2    E
5    3  NaN    F

In [98]: df['concatenated'] = df['col2'] +','+ df['col3']
In [99]: df
Out[99]: 
  col1 col2 col3 concatenated
0    1   p1    A         p1,A
1    1   p2    B         p2,B
2    2   p1    C         p1,C
3    2  NaN    D          NaN
4    3   p2    E         p2,E
5    3  NaN    F          NaN

Instead of 'NaN' values in "concatenated" column, I want to get "D" and "F" respectively for this example?

与“串联”列中的“NaN”值不同,我想在本例中分别得到“D”和“F”?

2 个解决方案

#1


15  

I don't think your problem is trivial. However, here is a workaround using numpy vectorization :

我不认为你的问题微不足道。然而,这里有一个使用numpy矢量化的解决方案:

In [49]: def concat(*args):
    ...:     strs = [str(arg) for arg in args if not pd.isnull(arg)]
    ...:     return ','.join(strs) if strs else np.nan
    ...: np_concat = np.vectorize(concat)
    ...: 

In [50]: np_concat(df['col2'], df['col3'])
Out[50]: 
array(['p1,A', 'p2,B', 'p1,C', 'D', 'p2,E', 'F'], 
      dtype='|S64')

In [51]: df['concatenated'] = np_concat(df['col2'], df['col3'])

In [52]: df
Out[52]: 
  col1 col2 col3 concatenated
0    1   p1    A         p1,A
1    1   p2    B         p2,B
2    2   p1    C         p1,C
3    2  NaN    D            D
4    3   p2    E         p2,E
5    3  NaN    F            F

[6 rows x 4 columns]

#2


7  

You could first replace NaNs with empty strings, for the whole dataframe or the column(s) you desire.

您可以首先用空字符串替换nan,用于整个dataframe或所需的列。

In [6]: df = df.fillna('')

In [7]: df['concatenated'] = df['col2'] +','+ df['col3']

In [8]: df
Out[8]:
  col1 col2 col3 concatenated
0    1   p1    A         p1,A
1    1   p2    B         p2,B
2    2   p1    C         p1,C
3    2         D           ,D
4    3   p2    E         p2,E
5    3         F           ,F

#1


15  

I don't think your problem is trivial. However, here is a workaround using numpy vectorization :

我不认为你的问题微不足道。然而,这里有一个使用numpy矢量化的解决方案:

In [49]: def concat(*args):
    ...:     strs = [str(arg) for arg in args if not pd.isnull(arg)]
    ...:     return ','.join(strs) if strs else np.nan
    ...: np_concat = np.vectorize(concat)
    ...: 

In [50]: np_concat(df['col2'], df['col3'])
Out[50]: 
array(['p1,A', 'p2,B', 'p1,C', 'D', 'p2,E', 'F'], 
      dtype='|S64')

In [51]: df['concatenated'] = np_concat(df['col2'], df['col3'])

In [52]: df
Out[52]: 
  col1 col2 col3 concatenated
0    1   p1    A         p1,A
1    1   p2    B         p2,B
2    2   p1    C         p1,C
3    2  NaN    D            D
4    3   p2    E         p2,E
5    3  NaN    F            F

[6 rows x 4 columns]

#2


7  

You could first replace NaNs with empty strings, for the whole dataframe or the column(s) you desire.

您可以首先用空字符串替换nan,用于整个dataframe或所需的列。

In [6]: df = df.fillna('')

In [7]: df['concatenated'] = df['col2'] +','+ df['col3']

In [8]: df
Out[8]:
  col1 col2 col3 concatenated
0    1   p1    A         p1,A
1    1   p2    B         p2,B
2    2   p1    C         p1,C
3    2         D           ,D
4    3   p2    E         p2,E
5    3         F           ,F