读入xlsx文件后没有返回列名 [英] Not getting back the column names after reading into an xlsx file

查看:253
本文介绍了读入xlsx文件后没有返回列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有xlsx文件,并使用pandas将它们合并为一个数据帧。它工作但不是取回我在xlsx文件中的列名,而是将数字作为列而不是列标题成为一行:像这样:

Hello I have xlsx files and merged them into one dataframe by using pandas. It worked but instead of getting back the column names that I had in the xlsx file I got numbers as columns instead and the column titles became a row: Like this:

 Output:      1           2           3
            COLTITLE1    COLTITLE2     COLTITLE3

它们应该是这样的:

Output: COLTITLE1     COLTITLE2      COLTITLE3

列标题不是列标题,而是它们已成为一行。如何获取xlsx文件中的正确列名。为清楚起见,xlsx文件中的所有列名都相同。帮助将不胜感激下面我的代码:

The column titles are not column titles but rather they have become a row. How can I get back the rightful column names that I had within the xlsx file. Just for clarity all the column names are the same within both the xlsx files. Help would be appreciated heres my code below:

# import modules
from IPython.display import display
import pandas as pd
import numpy as np
pd.set_option("display.max_rows", 999)
pd.set_option('max_colwidth',100)
%matplotlib inline

# filenames
file_names = ["data/OrderReport.xlsx", "data/OrderReport2.xlsx"]

# read them in
excels = [pd.ExcelFile(name) for name in file_names]

# turn them into dataframes
frames = [x.parse(x.sheet_names[0], header=None,index_col=None) for x in excels]

# concatenate them
atlantic_data = pd.concat(frames)

# write it out
combined.to_excel("c.xlsx", header=False, index=False)


推荐答案

<我希望我能正确理解你的问题。你只需要摆脱 index_col = None ,它将像往常一样返回列名:

I hope I understood your question correctly. You just need to get rid of the index_col=None and it will return the column name as usual:

frames = [x.parse(x.sheet_names[0], header=None) for x in excels]

如果添加 index_col =无 pandas会将您的列名称视为1行数据,而不是数据帧的列。

If you add index_col=None pandas will treat your column name as 1 row of data rather than a column for the dataframe.

这篇关于读入xlsx文件后没有返回列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆