pandas 列multiindex中缺少值 [英] missing values in pandas column multiindex

查看:48
本文介绍了 pandas 列multiindex中缺少值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读像这样的熊猫excel表格:

I am reading with pandas excel sheets like this one:

使用

df = pd.read_excel('./question.xlsx', sheet_name = None, header = [0,1])

这将导致具有多索引的多索引数据框.

which results in multiindex dataframe with multiindex.

这里引起问题的是,默认情况下,空字段用'Title'填充,而我更喜欢使用不同的标签.我无法跳过第一行,因为我正在处理较大的数据帧,其中第一行和第二行包含重复的标签(因此使用了multiindex).

What poses a problem here is that the empty fields are filled by default with 'Title', whereas I would prefer to use a distinct label. I cannot skip the first row since I am dealing with bigger data frames where the first and the second rows contain repeating labels (hence the use of the multiindex).

非常感谢您的帮助.

推荐答案

假设您要使用空字符串而不是重复第一个标签,则可以读取这两行并直接构建MultiIndex:

Assuming that you want to have empty strings instead of repeating the first label, you can read the 2 lines and build the MultiIndex directly:

df1 = pd.read_excel('./question.xlsx', header = None, nrows=2).fillna('')
index = pd.MultiIndex.from_arrays(df1.values)

它给出:

MultiIndex([('Title',        '#'),
            (     '',    'Price'),
            (     '', 'Quantity')],
           )

顺便说一句,如果您想为空字段使用不同的标签,则可以将其用作 fillna 的参数.

By the way, if you wanted a different label for empty fields, you can just use it as the parameter for fillna.

然后,您只需读取其余数据,然后手动设置索引:

Then, you just read the remaining data, and set the index by hand:

df1 = pd.read_excel('./question.xlsx', header = None, skiprows=2)
df1.columns = index

这篇关于 pandas 列multiindex中缺少值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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