简体   繁体   中英

If substring in df['column']: add value to another column

I have two columns in a dataframe as such:

Name Count
apple x2 1
banana 0
apple x3 1

If apple is present, the count is already set to 1. I need to extract is 'x' is present and then add the value after 'x' to the count column (it will be the value minus 1 to get the right amount).

Is there a way to do this vectorized without a loop?

I tried this loop:

for fruit in df['Name']:
    if fruit.str.contains('x', case = False, na = False):
        add = fruit[:-1]
        df['Count'] = df['Count'] + add - 1

I get this error: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

I think I am way off here...

import pandas as pd

You can use to_numeric() method and fillna() method and astype() method:

df['Count']=df['Count']+pd.to_numeric(df['Name'].str[-1],errors='coerce').fillna(0).astype(int)

Now If You print df you will get your desired output:

    Name        Count
0   apple x2    3
1   banana      0
2   apple x3    4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM