Home > Software design >  Fill NULL based on conditions python
Fill NULL based on conditions python

Time:02-11

I have a dataset, and needs to create a new column to deal with the NULL based on the conditions. Here is the sample data:

id1 id2 col1 col2 
1   1   2     3 
2   2   2     NULL
3   3   NULL  3
4   4   NULL  3
5   5   10    11      

The expect output is

id1 id2 col1 col2  combine
1   1   2     3     2,3
2   2   2     NULL  2,3
3   3   NULL  3     2,3
4   4   NULL  3     2,3
5   5   10    11    10,11

for example,If any col1 and col2 has 2 or 3, the new column need to contain both values.Not sure how to capture if col1 or col2 has NULLs.

CodePudding user response:

You can use ffill to fill NaNs convert to int, then str, then join on axis:

df['combine'] = df[['col1','col2']].ffill().astype(int).astype(str).apply(','.join, axis=1)

Output:

   id1  id2  col1  col2 combine
0    1    1   2.0   3.0     2,3
1    2    2   2.0   NaN     2,3
2    3    3   NaN   3.0     2,3
3    4    4  10.0  11.0   10,11
  • Related