Reputation: 16060
I wonder why the first code (that reads data from CSV) has a warning message SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame
, while the second code (that uses the same logic, but manual DataFrame initialization) does not have this warning message.
Code #1
import pandas as pd
myData = pd.read_csv('https://spark-public.s3.amazonaws.com/dataanalysis/loansData.csv')
for i in range(len(myData['Employment.Length'])):
myData['Employment.Length'][i] = 3
Code #2
list1 = ['bla', 'la', 'lal']
list2 = [1, 2, 3]
myData = pd.DataFrame({'1': list1, '2':list2})
for i in range(len(myData)):
myData['1'][i] = 3
Upvotes: 1
Views: 328
Reputation: 4499
Both the above codes generate the same warning. When both codes are run in same python application/console the warning is generated twice (once from each code) but it is reported only once due to the warnings filter being set to once
once: print only the first occurrence of matching warnings, regardless of location.
always: always print matching warnings
Refer documentation
To change this behavior set warnings filter to always
import warnings
warnings.filterwarnings("always")
Upvotes: 1