Pandas:当第一行不是列名 Excel 文件时读取 excel 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51733136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas: Reading excel files when the first row is NOT the column name Excel Files
提问by user7852656
I am using pandas to read an excel file. It doesn't have column name but it continues to read the first row as the column name.
我正在使用Pandas来读取 excel 文件。它没有列名,但它继续读取第一行作为列名。
Following is the excel file that is being read.
以下是正在读取的excel文件。
data1 0.994676
data2 0.994588
data3 0.99488
data4 0.994483
data5 0.994312
data6 0.993823
data7 0.993575
data8 0.994231
data9 0.993838
data10 0.994007
data11 0.994328
data12 0.993503
data13 0.99342
data14 0.992729
data15 0.993013
data16 0.993049
data17 0.993133
data18 0.99262
I'm reading the 2nd column using the following code. import pandas as pd
我正在使用以下代码阅读第二列。将Pandas导入为 pd
df=pd.ExcelFile('C:/Users/JohnDoe/Desktop/080718_output.xlsx', header=None, index_col=False).parse('Data_sheet')
y=df.iloc[0:17,1]
The following is the y.
以下是y。
In[38]:y
Out[38]:
0 0.994588
1 0.994880
2 0.994483
3 0.994312
4 0.993823
5 0.993575
6 0.994231
7 0.993838
8 0.994007
9 0.994328
10 0.993503
11 0.993420
12 0.992729
13 0.993013
14 0.993049
15 0.993133
16 0.992620
Name: 0.994676, dtype: float64
It skips the first data because the first row is being used as a column name.. Any idea on how I can improve this?
它跳过了第一个数据,因为第一行被用作列名。关于如何改进这一点有什么想法吗?
Edit: 'header=False' to 'header=None'. Both cases give the same outcome.
编辑:'header=False' 到 'header=None'。两种情况给出相同的结果。
回答by jezrael
You can use read_excel
with header=None
for default columns with rangeIndex
:
您可以将read_excel
withheader=None
用于默认列rangeIndex
:
df = pd.read_excel('file.xlsx',
sheet_name ='Data_sheet',
header=None,
index_col=False)
回答by Bram van Hout
Create a column header variable and call that in your excel read in statement as well as stating header=None
创建一个列标题变量并在你的 excel read in 语句中调用它,并声明 header=None
names=['Column1','Column2']
df=pd.read_excel(r"/Users/JohnDoe/Desktop/080718_output.xlsx",header=None,names=names)