Home > Net >  Pandas cuts off empty columns from csv file
Pandas cuts off empty columns from csv file

Time:11-10

I have the csv file that have columns with no content just headers. And I want them to be included to resulting DataFrame but pandas cuts them off by default. Is there any way to solve this by using read_csv not read_excell?

CodePudding user response:

IIUC, you need header=None:

from io import StringIO
import pandas as pd

data = """
not_header_1,not_header_2
"""
df = pd.read_csv(StringIO(data), sep=',')
print(df)

OUTPUT:

Empty DataFrame
Columns: [not_header_1, not_header_2]
Index: []

Now, with header=None

df = pd.read_csv(StringIO(data), sep=',', header=None)
print(df)

OUTPUT:

              0             1
0  not_header_1  not_header_2
  • Related