Steveman30290
Steveman30290

Reputation: 561

Joining Columns in Pandas Only if Both Values Are There

I have two pandas data frames I'm working with. The first one looks something like this (data_frame_1):

column 1        description
name1; name2    description1:hello
name3; name4    description2:hello
name23          description29:hi
name0292        description31:myself
name23          description21:yes
name3;name4     description18:yes

I've used the following code to split the first column by the semi-colon: Column_DF= table[0].str.split(';', expand=True)

I now want to take this new data frame, Column_DF, and join it with the description that is present on the same line of data_frame_1[1].

I know of the pd.merge function, but I can't seem to get it so it only merges (or joins) the rows that have values and not NANs.

Upvotes: 0

Views: 29

Answers (1)

Space Impact
Space Impact

Reputation: 13255

Use dropna with merge:

df = df.merge(df['column 1'].str.split(';', expand=True).dropna(), 
              left_index=True, right_index=True)

Or:

df = df.merge(df['column 1'].str.split(';', expand=True),
              left_index=True, right_index=True).dropna()

print(df)
       column 1         description      0       1
0  name1; name2  description1:hello  name1   name2
1  name3; name4  description2:hello  name3   name4
5   name3;name4   description18:yes  name3   name4

Upvotes: 1

Related Questions