Maneet Giri
Maneet Giri

Reputation: 185

Duplicate row if a column contains any special character in Python Pandas

I have written a small python program to deal with orders I receive. But due to change in the reference value I used to merge my df on is changed.

Example:

Data

The problem is, I used to merge Old PO numbers to new PO numbers so that I know a customer is returning. But recently I started receiving partial orders which are 3c456 instead of 3c456/4c345. It means my customer is renewing only one product now. My pd.merge code doesn't find the partial order anymore.

My code was

df = pd.merge(old_PO_file, New_PO_file, on =PO-number)

since the new PO is partial and could be the value before or after the slash. I would like to duplicate all the rows in PO-number column if there is a Slash(/) in that cell.

Please let me know if it is possible.

Thanks

Upvotes: 1

Views: 1356

Answers (1)

jezrael
jezrael

Reputation: 863116

You can use str.split with stack for creating Series for join to original DataFrame:

print (df)
   Sold-to    PO-Number Sold-to-party
0     1234       6d1234   abc company
1     2345  3c456/4c345   edf company
2     5679        9f639   mno company

df1 = df.drop('PO-Number', axis=1) \
        .join(df['PO-Number'] \
        .str \
        .split('/', expand=True) \
        .stack() \
        .reset_index(level=1, drop=True).rename('PO-Number')) \
        .reset_index(drop=True)

print (df1)
   Sold-to Sold-to-party PO-Number
0     1234   abc company    6d1234
1     2345   edf company     3c456
2     2345   edf company     4c345
3     5679   mno company     9f639

Upvotes: 2

Related Questions