Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pandas filters
#1
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
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020