Home > Mobile >  How do you replace values in one column with values in another when condition is not met with Python
How do you replace values in one column with values in another when condition is not met with Python

Time:01-13

I have some inconsistent data with 5 columns. The two I'm focussing on are - 'Account Name' and 'User ID'. All the Account names should contain a 7 character string. If it doesn't I want to replace if with the ID in 'User ID'. Example data:

Account Name User ID
JB05737 JB05737
bg@***.com BG49568
GV95577 GV95577

I want to get:

Account Name User ID
JB05737 JB05737
BG49568 BG49568
GV95577 GV95577

I have tried the following codes and got errors:

Reports.loc[Reports['Account Name'].astype(str).map(len) != 7] = Reports[Reports['User ID']].astype(str)

Reports.loc[Reports['Account Name'].astype(str).map(len) != 7].replace(Reports['User ID'])

    if (Reports.loc['Account Name'].map(len) != 7):
        Reports.loc[i, 'Account Name']==Reports.loc[i, 'User ID']```

```Reports.loc[Reports['Account Name'].map(len) != 7, 'Account Name] = Reports.loc[Reports['Account Name'].map(len) != 7,'User ID']```


The errors have included: shape errors, invalid syntax, ...not in index

Any suggestions?

CodePudding user response:

somthing like this..

for col1, id in df.itertuples(index=False):
  print("Iterating both columns:\n",a, b)
  if len(id) > 7:
    df['col1'].replace('col1', 'id')

CodePudding user response:

This code worked for me

Reports['Account Name']=Reports['User ID'].where(Reports['Account Name'].map(len)>7,Reports['User ID'])

  • Related