Getting rid of SettingWithCopyWarning in Python pandas
Question: 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?


Answer: > The chained assignment warnings / exceptions are aiming to inform the user
> of a possibly invalid assignment. There may be false positives; situations
> where a chained assignment is inadvertantly reported.

The purpose of this warning is to flag to the user that the assignment is
carried out on a copy of the DataFrame slice instead of the original Dataframe
itself.

You generally want to use .loc (or .iloc, .at, etc.) type indexing instead of
'chained' indexing which has the potential to not always work as expected.

To make it clear you only want to assign a copy of the data (versus a view of
the original slice) you can append .copy() to your request, e.g.

`df = data[columns].copy()`

See the [documentation](http://pandas.pydata.org/pandas-
docs/stable/indexing.html#returning-a-view-versus-a-copy) for more details.

