PriyankaJ
PriyankaJ

Reputation: 390

Python Pandas: Concatenate pandas strings containing null

I'm trying to concatenate multiple string columns containing null values. But result does not show concatenated string rather just first column. How can I ignore values containing null? I tried to handle this by replacing nan values but that does not help.

import pandas as pd
import numpy as np
df = pd.DataFrame({'Addr1':['a','b','c'], 'Addr2':['', 'x', np.nan], 'Addr3':['202', '201', '203']})
df1 = pd.DataFrame([])
df1['patient address'] = df['Addr1'].astype(str).fillna('',) + " " 
+ df['Addr2'].astype(str).fillna('') + " " 
+ df['Addr3'].astype(str).fillna('') 
print(df1)

Expected output:

      patient address
0              a 202
1              b x 201
2              c 203

Upvotes: 2

Views: 2310

Answers (2)

AMC
AMC

Reputation: 2702

import numpy as np
import pandas as pd

df = pd.DataFrame(
    {
        "Addr1": ["a", "b", "c"],
        "Addr2": ["", "x", np.nan],
        "Addr3": ["202", "201", "203"],
    }
)

df1 = pd.DataFrame(
    {
        "patient address": df[["Addr1", "Addr2", "Addr3"]]
        .fillna(" ")
        .agg(" ".join, axis=1)
    }
)

print(df1)

Output:

  patient address
0          a  202
1         b x 201
2         c   203

Upvotes: 0

anky
anky

Reputation: 75080

stack and groupby+agg with .join on level=0 , stack() by default drops the NaN:

df1['patient address'] = df.stack().groupby(level=0).agg(' '.join)

0     a  202
1    b x 201
2      c 203
dtype: object

Upvotes: 2

Related Questions