Dataframe How To Update A Column Based Many Str Values
I am creating a small financial management program which imports my transactions from CSV into Python. I want to assign values to a new column 'category' based on strings found in
Solution 1:
If you have one value, we can use str.extract
:
df['category'] = df['details'].str.extract(f'({str_xfer})')
amount details category
0-68.23 PAYPAL TRANSFER TRANSFER
1-12.46 RALPHS #0079 NaN2-8.51 SAVE AS YOU GO NaN325.34 VENMO CASHOUT NaN4-2.23 PAYPAL TRANSFER TRANSFER
5-64.29 PAYPAL TRANSFER TRANSFER
If you have multiple strings to match, we have to delimit your strings first by |
, which is the or operator in regular expressions.
str_xfer = ['TRANSFER', 'RALPHS', 'CASHOUT']
str_xfer = '|'.join(str_xfer)
df['category'] = df['details'].str.extract(f'({str_xfer})')
amountdetailscategory0-68.23PAYPALTRANSFERTRANSFER1-12.46RALPHS#0079RALPHS2-8.51SAVEASYOUGONaN325.34VENMOCASHOUTCASHOUT4-2.23PAYPALTRANSFERTRANSFER5-64.29PAYPALTRANSFERTRANSFER
Solution 2:
I think you need str.findall
df['category']=df.details.str.findall('TRANSFER').str[0].fillna(0)
df
amount details category
0 -68.23 PAYPAL TRANSFER TRANSFER
1 -12.46 RALPHS #0079 0
2 -8.51 SAVE AS YOU GO 0
3 25.34 VENMO CASHOUT 0
4 -2.23 PAYPAL TRANSFER TRANSFER
5 -64.29 PAYPAL TRANSFER TRANSFER
If you have more than one string in str_xfer
adding '|'
df.details.str.findall('TRANSFER|VENMO').str[0]0TRANSFER1NaN2NaN3VENMO4TRANSFER5TRANSFERName: details, dtype: object
Post a Comment for "Dataframe How To Update A Column Based Many Str Values"