【问题标题】:Python Pandas Reshape Data FramePython Pandas 重塑数据框
【发布时间】:2020-04-19 07:09:18
【问题描述】:

这似乎是非常基础的知识,但尽管有一些数据处理的理论背景(通过其他软件),但我还是被卡住了。值得一提的是,我是 python 和 pandas 库的新手。

所以。我有一个数据框:

我的任务是将“系列名称”列的值作为单独的列(从长转换为宽)。我花了很长时间尝试不同的方法,但只得到错误。

例如:

mydata = mydata.pivot(index=['Country', 'Year'], columns='Series Name', values='Value')

我得到了一个错误:

...很多文字... ValueError: 传递值的长度是 2487175,索引意味着 2

有人可以指导我完成这个过程吗?谢谢。

这是为了代码 'mydata = mydata.pivot(index=['Country', 'Year'], columns='系列名称', values='Value')' 错误信息:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-8169d6d374c7> in <module>
----> 1 mydata = mydata.pivot(index=['Country', 'Year'], columns='Series Name', values='Value')

~/anaconda3_501/lib/python3.6/site-packages/pandas/core/frame.py in pivot(self, index, columns, values)
   5192         """
   5193         from pandas.core.reshape.reshape import pivot
-> 5194         return pivot(self, index=index, columns=columns, values=values)
   5195 
   5196     _shared_docs['pivot_table'] = """

~/anaconda3_501/lib/python3.6/site-packages/pandas/core/reshape/reshape.py in pivot(self, index, columns, values)
    412         else:
    413             indexed = self._constructor_sliced(self[values].values,
--> 414                                                index=index)
    415     return indexed.unstack(columns)
    416 

~/anaconda3_501/lib/python3.6/site-packages/pandas/core/series.py in __init__(self, data, index, dtype, name, copy, fastpath)
    260                             'Length of passed values is {val}, '
    261                             'index implies {ind}'
--> 262                             .format(val=len(data), ind=len(index)))
    263                 except TypeError:
    264                     pass

ValueError: Length of passed values is 2487175, index implies 2

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    试试吧:

    mydata = mydata.pivot_table(index=['Country', 'Year'], columns='Series Name', values='Value', aggfunc='sum')
    

    (如果你想总结你的Value)似乎你需要以某种方式明确地聚合你的数据。 虽然会很好,但如果你能分享完整的错误信息。

    我设法重现了您的错误。就像我说的 - 你需要提供聚合功能:

    import pandas as pd
    
    df=pd.DataFrame({"a": list("xyzpqr"), "b": list("abbbaa"), "c": [4,3,6,2,7,5], "d": list("pqqppp")})
    
    df2=df.pivot(index=["b", "d"], columns="a", values="c")
    #ValueError: Length of passed values is 6, index implies 2
    
    df2=df.pivot_table(index=["b", "d"], columns="a", values="c", aggfunc=set)
    #works fine - you need aggregation function e.g. list/set to collect all/unique values or e.g. sum/max to do some numeric operation
    

    【讨论】:

    • 实际上我不希望对数据进行任何聚合。每个国家/年都有独特的价值,我想保持原样。我试过你的代码,笔记本的内核太忙了,所以我打断了它……会不会是数据类型错误之类的?稍后我会用更小的数据集尝试你的代码......
    • @Leo 如果您确定,您将在聚合中获得唯一值(即您想摆脱“括号”),只需执行以下操作:mydata = mydata.pivot_table(index=['Country', 'Year'], columns='Series Name', values='Value', aggfunc='first')
    【解决方案2】:

    快到了。结果表为

    如何将'Country'和'Year'与其他列名放在同一级别才能正常导出到excel?如果我像现在这样导出,表格中不包含“国家”和“年份”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-10
      • 2020-10-21
      • 2017-04-23
      • 2020-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多