0

When I read csv from file, due to the error of the data file, sometimes there will be unnecessary column at the end, so I read them as

df = pd.read_csv(StringIO(all_data_file), skipinitialspace=True, sep=' ', error_bad_lines=False, names = ['stationID', .... ,'unknown'])

Later on I throw the data if the unknow has data

# Drop if has too many field
df = df[df['unknown'].isnull()]
df.drop('unknown', axis=1, inplace=True)

The problem is that, it gives me error

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

So I want to know, what is the proper approach for dropping the data?

ZK Zhao
  • 19,885
  • 47
  • 132
  • 206
  • It seems ok, maybe you can use one line `df = df[df['unknown'].isnull()].drop('unknown', axis=1)` – jezrael Mar 23 '17 at 08:45
  • Change `df = df[df['unknown'].isnull()]` to `df = df[df['unknown'].isnull()].copy()` or set `df.is_copy = None` after the assignment. – ayhan Mar 23 '17 at 08:45
  • FWIW, `SettingWithCopyWarning` is a warning and not an error http://stackoverflow.com/q/20625582/2327328 – philshem Mar 23 '17 at 08:46

0 Answers0