Here is an example:
cars2 = {'Brand': ['Hon*da\nCivic', 'BM^AMT^B6^278.99\n'],
'Price': [22000, 55000]
}
df2 = pd.DataFrame(cars2, columns = ['Brand', 'Price'])
delim = '^'
Output:
Brand Price
0 Hon*da\nCivic 22000
1 BM^AMT^B6^278.99\n 55000
I need:
Brand Price Allowed Amount
0 Hon*da\nCivic 22000 0
1 BM^AMT^B6^278.99\n 55000 278.99
I tried:
for field in range(0, len(df2)):
if 'AMT'+delim+'B6' in df2.iloc[field]['Brand']:
df2.iloc[field]['Allowed_Amount'] = df2.iloc[field]['Brand'].split("AMT"+delim+"B6")[1][1:].split('\n')[0]
else:
df2.iloc[field]['Allowed_Amount'] = 0
It gives me this error:
__main__:5: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
How can i fix this? If there is a better way to do this, please let me know. Thanks.
You can do like this:
df2['Allowed_Amount'] = df2['Brand'].str.split('^',-1).str[3]
df2['Allowed_Amount'] = df2['Allowed_Amount'].str[:-2]
You can also make it one line:
df2['Allowed_Amount'] = df2['Brand'].str.split('^',-1).str[3].str[:-2]
The first line will split the "^" by doing from last and get the last value. Then in second line we removing the new line !
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.