简体   繁体   English

使用熊猫读取CSV文件时,左对齐标题

[英]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. 我正在读取.csv文件并应用标题,该标题是我想要的列名的列表。

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. 如果.csv文件的列数多于“标题”列表中的名称,则列名将自动右对齐,以便第一列或左列标题为空白。 Is there any way to left justify when applying the header to the DataFrame? 将标头应用于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 考虑csv的文本以及随后的读取文本的调用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM