Pandas SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame

1

I want to copy an element from a dataframe and insert it into another dataframe.

In essence there is a dataframe with name x area and another that I need to load with the data of the area, from the comparison of the name in the first dataframe. The code is working, but give this title warning.

What I'm doing:

for i in range(0,len(NOVOS_ABERTOS_VIVO)):
    for j in range(0,len(AREA_VERSUS_COLABORADOR)):
        if NOVOS_ABERTOS_VIVO.loc[i]['Originator'] == AREA_VERSUS_COLABORADOR.loc[j]['Originator']:
            TEMP=AREA_VERSUS_COLABORADOR.loc[j]['Área']
            NOVOS_ABERTOS_VIVO.loc[i]['Área']=str(TEMP)

I also did:

for i in range(0,len(NOVOS_ABERTOS_VIVO)):
    for j in range(0,len(AREA_VERSUS_COLABORADOR)):
        if NOVOS_ABERTOS_VIVO.loc[i]['Originator'] == AREA_VERSUS_COLABORADOR.loc[j]['Originator']:
            AREA_VERSUS_COLABORADOR.loc[j]['Área']=NOVOS_ABERTOS_VIVO.loc[i]['Área'] 
    
asked by anonymous 18.09.2018 / 03:05

1 answer

2

Hello, Guido. Welcome!

This warning can be easily solved by modifying your code like this:

for i in range(0,len(NOVOS_ABERTOS_VIVO)):
    for j in range(0,len(AREA_VERSUS_COLABORADOR)):
        if NOVOS_ABERTOS_VIVO.loc[i]['Originator'] == AREA_VERSUS_COLABORADOR.loc[j]['Originator']:
            AREA_VERSUS_COLABORADOR.loc[j,'Área']=NOVOS_ABERTOS_VIVO.loc[i,'Área']

Note that instead of typing df.loc[linha][coluna] , df.loc[linha,coluna] was written. This is the recommended way to do this type of operation for the sake of performance.

In the first form ( df.loc[linha][coluna] ) what is being done is a linear and operations independent call, or a chain call. First, the pandas returns all columns of the dataframe of the desired row, then returns the column.

On the other hand, the second form ( df.loc[linha,coluna] ) allows pandas to treat your search as a single entity, greatly improving speed.

References:

link

    
18.09.2018 / 23:50