Skip to content
Advertisement

Using Pandas df.loc

I have a DataFrame of a csv file which is being read by pandas. What I am attempting to do is use df.loc to add a new column but only insert values into the column when values from another column, called “SKU” end with “-RF” and “-NEW”.

The code I was working on is below. It has the csv file being read, “original_file”, and then two .loc commands. ONE new column called “Cond” is supposed to be being added, and I want “NEW” to be added to that column where the value in the ‘SKU’ column ends with “-NEW” and “USED” to be added to the new column where the ‘SKU’ ends with “-RF”.

AttributeError: ‘list’ object has no attribute ‘str’

This is the error I have been getting on it.

original_file = pd.read_csv('wc-product-export-20-9-2022-1663680891149.csv')

original_file.loc[['SKU'].str.endswith("-RF"),'Cond'] = 'USED'
original_file.loc[['SKU'].str.endswith("-NEW"),'Cond'] = 'NEW'

Advertisement

Answer

Try with adding the df when you call column by name

original_file.loc[original_file['SKU'].str.endswith("-RF"),'Cond'] = 'USED'
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement