I am loading a bunch of csvs and processing certain columns if they exist, after loading the csv with pandas
data = pd.read_csv('Test.csv', encoding = "ISO-8859-1", index_col=0)
this dataframe will be used in the example
import pandas as pd
data = pd.DataFrame({'A': [1, 2.1, 0, 4.7, 5.6, 6.8],
'B': [0, 1, 0, 0, 0, 0],
'C': [0, 0, 0, 0, 0, 1],
'D': [5, 5, 6, 5, 5.6, 6.8],
'E': [2, 4, 1, 0, 0, 5],
'F': [0, 0, 1, 0, 0, 0],
'G': [0, 0, 0, 0, 0, 0],})
Next I check and select specific columns that are going be processed
coltitles = ['A', 'B','C', 'D', 'E']
columns = []
for name in coltitles:
if name in data.columns:
columns.append(name)
else:
print (name, 'is missing')
df = data[columns]
if 'A' in df.columns:
#perform some processing, I will put print to simplify it
print ('Exist')
The code works if I use a dataframe for data, but If I load the data from a csv I get a Warning:
<module3>:74: 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 the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
The warning is caused by the line where df = data[columns].
The code still works with the warning but how do I get rid of this warning without suppressing it?
coltitles = ['A', 'B','C', 'D', 'E']? What iscolumnswhen the exception happens?data[columns] = somethingfor example. @ccsv are you sure the error is on the line that saysdf = data[columns]-- in which case, could you please paste a full working snippet that reproduces the error?