Python Forum

Full Version: Pandas filters
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi

i have two columns ID and description.

If the ID is blank, and the description contains as P2.... then ID should come as P2 in place of balnk.

I tried it with below logic.. did not work.

df1.loc[len(df1['ID']=='0') & (df1['Description'].str.contains('(?i)P2')),df1['Description'].str[-2:]]

Data :

ID Description
P1 Aw| Large | Product| P1
P3 kw| small | Product| P3
Nan Tw| Medium | Product| P2

Output:

ID
P1
P3
P2

The Nan in ID column should fill with P2 based on the description.

Please help with logic