【问题标题】:Appending multi-indexed Series to an empty Series in pandas将多索引系列附加到熊猫中的空系列
【发布时间】:2014-07-11 11:50:09
【问题描述】:

我采用了一个名为“dat”的多索引熊猫系列,并尝试将其附加到一个名为“s”的空系列中。这就是“dat”的样子:

a = [['bar', 'bar', 'foo', 'foo'],['one', 'two', 'one', 'two']]
t = list(zip(*a))
ind = pd.MultiIndex.from_tuples(t, names=['first', 'second'])
dat = pd.Series(randn(4), index=ind)

Out[1]: 
first  second
bar    one      -1.361606
       two      -0.108458
foo    one      -0.691175
       two      -0.830161
dtype: float64

现在,当我将它附加到空的 Series 's' 时,它会返回:

s = pd.Series()
s = s.append(dat)
Out[2]: 
(bar, one)   -1.361606
(bar, two)   -0.108458
(foo, one)   -0.691175
(foo, two)   -0.830161
dtype: float64

如何恢复原始表单?

【问题讨论】:

    标签: python pandas append series


    【解决方案1】:

    这是在 0.14.0(下周发布)中修复的错误,请参阅此处:http://pandas-docs.github.io/pandas-docs-travis/whatsnew.html#whatsnew-0140-api(set_index 部分)。

    您可以在此处获取预发布:https://github.com/pydata/pandas/releases

    【讨论】:

    • 太棒了!它现在有效......我几乎为此发疯了。谢谢!
    • @Jeff 这个错误似乎已在 18.1 中重新引入。你知道最初的问题报告吗?
    • 你必须展示一个例子;我们有这个特定的测试
    • 问题中的代码在 18.1 中创建了单级索引,但在 18.0 中保留了多级索引。
    猜你喜欢
    • 2018-02-28
    • 1970-01-01
    • 1970-01-01
    • 2019-10-01
    • 2018-03-25
    • 2018-12-02
    • 2018-12-25
    • 2019-11-07
    • 2015-06-16
    相关资源
    最近更新 更多