我有一个Excel文件,其索引在Excel中的几行合并,当我在pandas中加载它时,它将第一行作为索引标签读取,其余的(合并的单元格)用NaN填充。如何循环索引以使其用相应的索引填充NaN?
编辑:请求删除excel的图像。我没有任何具体的代码,但我可以写一个例子。
import pandas as pd
df = pd.read_excel('myexcelfile.xlsx', header=1)
df.head()
Index-header Month
0 Index1 1
1 NaN 2
2 NaN 3
3 NaN 4
4 NaN 5
5 Index2 1
6 NaN 2
...
答案 0 :(得分:4)
试试这个:
In [205]: df
Out[205]:
Index-header Month
0 Index1 1.0
1 NaN 2.0
2 NaN 3.0
3 NaN 4.0
4 NaN 5.0
5 Index2 1.0
6 NaN 2.0
... NaN NaN
In [206]: df['Index-header'] = df['Index-header'].fillna(method='pad')
In [207]: df
Out[207]:
Index-header Month
0 Index1 1.0
1 Index1 2.0
2 Index1 3.0
3 Index1 4.0
4 Index1 5.0
5 Index2 1.0
6 Index2 2.0
... Index2 NaN
答案 1 :(得分:2)