pandas ,将DataFrame转换为MultiIndex'ed DataFrame [英] pandas, convert DataFrame to MultiIndex'ed DataFrame

查看:698
本文介绍了 pandas ,将DataFrame转换为MultiIndex'ed DataFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要转换为MultiIndex ed pandas.DataFramepandas.DataFrame.

I have a pandas.DataFrame that I want to convert to a MultiIndexed pandas.DataFrame.

import numpy
import pandas
import itertools

xs = numpy.linspace(0, 10, 100)
ys = numpy.linspace(0, 0.1, 20)
zs = numpy.linspace(0, 5, 200)

def func(x, y, z):
    return x * y / z

vals = list(itertools.product(xs, ys, zs))
result = [func(x, y, z) for x, y, z in vals]

# Original DataFrame.
df = pandas.DataFrame(vals, columns=['x', 'y', 'z'])
df = pandas.concat((pandas.DataFrame(result, columns=['result']), df), axis=1)

# I want to turn `df` into this `df2`.
index = pandas.MultiIndex.from_tuples(vals, names=['x', 'y', 'z'])
df2 = pandas.DataFrame(result, columns=['result'], index=index)

请注意,在此示例中,我创建了想要拥有的内容.

Note that in this example I create what I want and what I have.

因此,IRL我将从df开始并想将其转换为df2(并且无权访问valsresult),我该怎么做?

So, IRL I would start with df and want to turn it into df2 (and don't have access to vals and result), how do I do this?

推荐答案

您需要 set_index :

print (df2.head())
                  result
x   y   z               
0.0 0.0 0.000000     NaN
        0.025126     0.0
        0.050251     0.0
        0.075377     0.0
        0.100503     0.0

print (df.set_index(['x','y','z']).head())

                  result
x   y   z               
0.0 0.0 0.000000     NaN
        0.025126     0.0
        0.050251     0.0
        0.075377     0.0
        0.100503     0.0

如果需要比较两个DataFrames,则需要将NaN替换为相同的值,否则获取False:

If need compare both DataFrames, need replace NaN to same values, else get False:

print (df.set_index(['x','y','z']).eq(df2).all())
result    False
dtype: bool

print (np.nan == np.nan)
False

print (df.fillna(1).set_index(['x','y','z']).eq(df2.fillna(1)).all())
result    True
dtype: bool

这篇关于 pandas ,将DataFrame转换为MultiIndex'ed DataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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