简体   繁体   中英

Left justify headers when reading a CSV file using pandas

I am reading .csv files and applying a header which is a list of my desired column names.

df=pd.read_csv(myfile,names=header)

If the .csv file has more columns than names in "header" list then the column names are automatically right justified so that the first or left column headers are blank. Is there any way to left justify when applying the header to the DataFrame? Right now what I'm doing is padding the "header" list with blank columns at the end as a workaround like this:

header = ['col1','col2','col3','','',]

I don't believe pandas supports this feature. However, I think a good workaround would be:

header = ['col1', 'col2', 'col3']

df = pd.read_csv(myfile)
df.columns = header + [''] * (len(df.columns) - len(header))

This way, you remove the need to hardcode your padding.

You can use a generator that starts with your prescribed columns then proceeds to yield '' for infinity. Use this to rename your columns.

Consider the text in csv and the subsequent call to read it

import pandas as pd
from itertools import chain, repeat

csv = """a1,b1,c1,d1
a2,b2,c2,d2"""

pd.read_csv(pd.io.common.StringIO(csv), header=None).rename(
    columns=lambda x, c=chain(['a', 'b'], repeat('')): next(c)
)

    a   b        
0  a1  b1  c1  d1
1  a2  b2  c2  d2

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