简体   繁体   中英

Weird SettingWithCopyWarning in pandas

I read lot of posts and information about that error in pandas. I try to write a code in different way, but nothing helps and it's still unclear to me.

I have a dataframe - df1 with three cols:

SampleIdInt, Username, Signature 

When I try to add a column filled '1' to df I got an SettingWithCopyWarning. Code:

df1['PolName'] = 1

In my another script that code didn't throw error, so why in this case i got it? I have just delared dataframe and dictionary.

Next, when I try to translate values by a dictionary it again throw a error. Code:

df1.loc[:,'PolName'] = df1['SampleIdInt'].apply(lambda y:slownik[y] if y in slownik.keys() else 'None')

I tried with loc, iloc, with different code syntax. Everytime I gor an error. What is weird? Somethimes I got an error and code modified a df anyway, sometimes I got an error, and df stay unchaged - I don't change anything in that code.

Can someone explain me what is a problem, exactly on above example?

Important parts of code. Could be useful:

baza = pd.read_csv('Zeszyt1.csv', sep = ';')
snps = pd.read_csv('snps.csv', header = None, low_memory = False, sep = ',')

vals = snps.loc[snps.duplicated(['SampleIdInt',5]), 'SampleIdInt'].unique()

mask = snps['SampleIdInt'].isin(vals)

df1 = snps[~mask]

What you can do is:

df1['PolName'] = df1['SampleIdInt'].map(slownik).fillna('None')

Also you can add copy() to df1 assignment:

df1 = snps[~mask].copy()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM