用pandas和多个索引读取python中的excel文件

2024-09-28 05:26:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一个python新手,请原谅这个基本问题。 我的.xlsx文件如下所示

Unnamend:1    A     Unnamend:2    B
2015-01-01    10    2015-01-01    10
2015-01-02    20    2015-01-01    20
2015-01-03    30    NaT           NaN

当我在Python中使用pandas.read_excel(…)pandas自动使用第一列作为时间索引。在

是否有一个行程序告诉pandas注意,每一个第二列都是属于它旁边的时间序列的时间索引?在

期望的输出如下所示:

^{pr2}$

Tags: 文件程序pandasread时间序列nanxlsx
2条回答

为了解析相邻的columns的块并在它们各自的datetime索引上对齐,可以执行以下操作:

df开头:

Int64Index: 3 entries, 0 to 2
Data columns (total 4 columns):
Unnamed: 0    3 non-null datetime64[ns]
A             3 non-null int64
Unnamed: 1    2 non-null datetime64[ns]
B             2 non-null float64
dtypes: datetime64[ns](2), float64(1), int64(1)

您可以在两列的块上迭代mergeindex上迭代,如下所示:

^{pr2}$

获得:

             A   B
2015-01-01  10  10
2015-01-01  10  20
2015-01-02  20 NaN
2015-01-03  30 NaN

不幸的是,pd.concat不起作用,因为它不能处理重复的index项,否则可以使用list comprehension

pd.concat([df.loc[:, cols].set_index(cols[0]) for cols in chunks(list(df), 2)], axis=1)

在使用pandas显示之后,我使用xlrd导入数据

import xlrd
import pandas as pd
workbook = xlrd.open_workbook(xls_name)
workbook = xlrd.open_workbook(xls_name, encoding_override="cp1252")
worksheet = workbook.sheet_by_index(0)
first_row = [] # The row where we stock the name of the column
for col in range(worksheet.ncols):
    first_row.append( worksheet.cell_value(0,col) )
data =[]
for row in range(10, worksheet.nrows):
    elm = {}
    for col in range(worksheet.ncols):
          elm[first_row[col]]=worksheet.cell_value(row,col)
    data.append(elm)

first_column=second_column=third_column=[]
for elm in data :
    first_column.append(elm(first_row[0]))
    second_column.append(elm(first_row[1]))
    third_column.append(elm(first_row[2]))

dict1={}
dict1[first_row[0]]=first_column
dict1[first_row[1]]=second_column
dict1[first_row[2]]=third_column
res=pd.DataFrame(dict1, columns=['column1', 'column2', 'column3'])
print res

相关问题 更多 >

    热门问题