python Pandas : multi-index unstack taking forever

标签 python pandas dataframe

我从 .csv 文件中读取了一个 DataFrame,其中包含以下列:

columns = ['Year', 'month', 'column1', 'column2','column3', 'column4', 'column5', 'column6', 'column7', 'column8','Value']

dataframe 有 116408 行,但在 df = df.drop_duplicates() 之后它现在有 98829(我不知道为什么它首先有重复项)

我需要像这样拆开它:

                         1              2              3              ....
                         2016 2017 2018 2016 2017 2018 2016 2017 2018 .... 
column1 column2 .......  
     a1      a2    ...     24   12   20   22   15   21   12   11   13  ...
     b1      b2    ...     18   11   21   21   11   31   14   41   14  ...

到目前为止,我已经尝试过:

df = df.set_index(columns[:-1], append=True)
df = df.unstack(level=[0,1])

但这需要永远。 (如果我删除 append 会出现此错误:ValueError: Index contains duplicate entries, cannot reshape)

有没有人有其他选择或知道为什么要花这么长时间? 我还没有看到结果,也没有看到任何错误。

最佳答案

我相信您拆错了层级。因为你在设置索引时有 append=True ,所以你的新索引中的第一个值就是它原来的值(你没有指出这个索引值是什么,所以我只是假设一个连续的范围从零开始)。接下来的两个级别将是 Yearmonth

所以,试试这个来获得你想要的输出:

df.unstack(level=[1, 2])

np.random.seed(0)
columns = ['Year', 'month', 'column1', 'column2', 'column3', 'column4', 'column5', 'column6', 'column7', 'column8','Value']
df = pd.DataFrame(np.random.randn(99, 11), columns=columns)
df.loc[:, 'Year'] = [2016, 2017, 2018] * 33
df.loc[:, 'month'] = [1, 2, 3] * 33

>>> df.set_index(columns[:-1], append=True).unstack(level=[1,2]).head()
                                                                                      Value  \
Year                                                                                   2016   
month                                                                                     1   
  column1   column2   column3   column4   column5   column6   column7   column8               
0  0.978738  2.240893  1.867558 -0.977278  0.950088 -0.151357 -0.103219  0.410599  0.144044   
1  0.121675  0.443863  0.333674  1.494079 -0.205158  0.313068 -0.854096 -2.552990       NaN   
2  2.269755 -1.454366  0.045759 -0.187184  1.532779  1.469359  0.154947  0.378163       NaN   
3  0.156349  1.230291  1.202380 -0.387327 -0.302303 -1.048553 -1.420018 -1.706270  1.950775   
4 -1.252795  0.777490 -1.613898 -0.212740 -0.895467  0.386902 -0.510805 -1.180632       NaN   

                                                                                             \
Year                                                                                   2017   
month                                                                                     2   
  column1   column2   column3   column4   column5   column6   column7   column8               
0  0.978738  2.240893  1.867558 -0.977278  0.950088 -0.151357 -0.103219  0.410599       NaN   
1  0.121675  0.443863  0.333674  1.494079 -0.205158  0.313068 -0.854096 -2.552990  0.653619   
2  2.269755 -1.454366  0.045759 -0.187184  1.532779  1.469359  0.154947  0.378163       NaN   
3  0.156349  1.230291  1.202380 -0.387327 -0.302303 -1.048553 -1.420018 -1.706270       NaN   
4 -1.252795  0.777490 -1.613898 -0.212740 -0.895467  0.386902 -0.510805 -1.180632 -0.028182   


Year                                                                                   2018  
month                                                                                     3  
  column1   column2   column3   column4   column5   column6   column7   column8              
0  0.978738  2.240893  1.867558 -0.977278  0.950088 -0.151357 -0.103219  0.410599       NaN  
1  0.121675  0.443863  0.333674  1.494079 -0.205158  0.313068 -0.854096 -2.552990       NaN  
2  2.269755 -1.454366  0.045759 -0.187184  1.532779  1.469359  0.154947  0.378163 -0.887786  
3  0.156349  1.230291  1.202380 -0.387327 -0.302303 -1.048553 -1.420018 -1.706270       NaN  
4 -1.252795  0.777490 -1.613898 -0.212740 -0.895467  0.386902 -0.510805 -1.180632       NaN 

关于 python Pandas : multi-index unstack taking forever,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46592104/

相关文章:

python - 创建一个类来生成子图

python - 将 csv 转换为 json,用于 Python 或 PHP 中的 JavaScript 绘图库 Chartjs

python - Pandas:按月份划分的词频

python-3.x - 如何根据列表中的单词创建新的 Pandas 列

python - IOError: [Errno 22] 传入的时钟() 参数无效

python - 根据另一列的唯一值求和列的值

python - 读取 csv 文件中重复的列名

python - 使用 pandas 解析大量日期 - 可扩展性 - 性能下降速度快于线性

python - 如何根据列前缀拆分 pandas 数据框

python - 与 Numba 并行循环——与 prange 不并行