1

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','','',]

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.