0

This stripes out the unwanted white space from the column headings when we parse from an excel file with multiple sheets.

df = {key: sheet.rename(columns=lambda x: x.strip()) for key, sheet in pd.read_excel(filename).items()}

But if I want to parse from a csv file instead, and not from an excel with sheets, how do I do this?

df = pd.read_csv(fname, sep=";", header=0, encoding="iso-8859-1", error_bad_lines=False)

keys_df = df.rename(columns={lambda x: x.strip()})

doesn't seem to work.

I want to do it in dictionary keys, not a list.

This question did not help me, as I want to create dictionary keys from the renamed columns.

joasa
  • 946
  • 4
  • 15
  • 35
  • please give us a sample of filename.csv and your expected output. – jose_bacoy Jun 05 '19 at 15:46
  • `pd.read_csv(...)` returns a `DataFrame`. CSV files don't have sheets so you don't get a `dict` item back from it. So when you apply `.items()` to it it's `DataFrame.items()` you're trying to use and that's not going to work... Are you trying to treat a load of CSV files each as sheets or...? – Jon Clements Jun 05 '19 at 15:48
  • What do you want to do with the dictionary? I spy with my little eye, an XY problem – cs95 Jun 05 '19 at 15:52
  • I want to stripe out the unwanted white space from the column headings when I parse from a csv file. – joasa Jun 05 '19 at 15:54
  • I updated the code with what I'm trying now but I get the error at the `keys_df` line : `TypeError: 'set' object is not callable` – joasa Jun 05 '19 at 15:59
  • first rename the columns `df.columns = [x.strip() for x in df.columns]` and then assign it to keys_df as `keys_df = {x:[] for x in df.columns}` – vb_rises Jun 05 '19 at 16:09

1 Answers1

0

If you want to extract a dataframe from a csv file and strip the column names:

df = pd.read_csv(fname, sep=";", header=0, encoding="iso-8859-1",
                 error_bad_lines=False).rename(columns=lambda x: x.strip())
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252