based on the text in 'column_name', I want to create a row in a new column that returns the string associated with the text found in 'column_name'. Here is the code:
temp_2 = []
for index,row in df2.iterrows():
if 'abc' in row['column_name']:
temp_2.append('A')
elif 'def'in row['column_name']:
temp_2.append('B')
elif 'ghk'in row['column_name']:
temp_2.append('C')
else:
temp_2.append('Other')
df2['Temp_2'] = temp_2
df2
Python returns:
/tmp/ipykernel_33624/2126258456.py:20: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
df2['Temp_2'] = temp_2
How can I modify the code to remove the warning?
CodePudding user response:
For remove warnings :-)
import warnings
warnings.filterwarnings("ignore")
for cleaner code you can try this :-)
df['Temp_2']='Other'
df['Temp_2'][df.column_name.str.contains('abc')]='A'
df['Temp_2'][df.column_name.str.contains('def')]='B'
df['Temp_2'][df.column_name.str.contains('ghk')]='C'
df