【问题标题】:Pandas Modify Dataframe熊猫修改数据框
【发布时间】:2016-12-13 04:41:57
【问题描述】:

我有一个如下的数据框

           0         1      2         3         4         5
    0  0.428519  0.000000  0.0  0.541096  0.250099  0.345604
    1  0.056650  0.000000  0.0  0.000000  0.000000  0.000000
    2  0.000000  0.000000  0.0  0.000000  0.000000  0.000000
    3  0.849066  0.559117  0.0  0.374447  0.424247  0.586254
    4  0.317644  0.000000  0.0  0.271171  0.586686  0.424560

我想修改如下

    0      0     0.428519
    0      1     0.000000
    0      2     0.0
    0      3     0.541096
    0      4     0.250099
    0      5     0.345604
    1      0     0.056650
    1      1     0.000000
    ........

【问题讨论】:

    标签: python-3.x pandas


    【解决方案1】:

    stackreset_index 一起使用:

    df1 = df.stack().reset_index()
    df1.columns = ['col1','col2','col3']
    
    print (df1)
        col1 col2      col3
    0      0    0  0.428519
    1      0    1  0.000000
    2      0    2  0.000000
    3      0    3  0.541096
    4      0    4  0.250099
    5      0    5  0.345604
    6      1    0  0.056650
    7      1    1  0.000000
    8      1    2  0.000000
    9      1    3  0.000000
    10     1    4  0.000000
    11     1    5  0.000000
    12     2    0  0.000000
    13     2    1  0.000000
    14     2    2  0.000000
    15     2    3  0.000000
    16     2    4  0.000000
    17     2    5  0.000000
    18     3    0  0.849066
    19     3    1  0.559117
    20     3    2  0.000000
    21     3    3  0.374447
    22     3    4  0.424247
    23     3    5  0.586254
    24     4    0  0.317644
    25     4    1  0.000000
    26     4    2  0.000000
    27     4    3  0.271171
    28     4    4  0.586686
    29     4    5  0.424560
    

    具有numpy.tilenumpy.repeat 的Numpy 解决方案,由numpy.ravel 进行展平:

    df2 = pd.DataFrame({
            "col1": np.repeat(df.index, len(df.columns)),
            "col2": np.tile(df.columns, len(df.index)),
            "col3": df.values.ravel()})
    
    print (df2) 
        col1 col2      col3
    0      0    0  0.428519
    1      0    1  0.000000
    2      0    2  0.000000
    3      0    3  0.541096
    4      0    4  0.250099
    5      0    5  0.345604
    6      1    0  0.056650
    7      1    1  0.000000
    8      1    2  0.000000
    9      1    3  0.000000
    10     1    4  0.000000
    11     1    5  0.000000
    12     2    0  0.000000
    13     2    1  0.000000
    14     2    2  0.000000
    15     2    3  0.000000
    16     2    4  0.000000
    17     2    5  0.000000
    18     3    0  0.849066
    19     3    1  0.559117
    20     3    2  0.000000
    21     3    3  0.374447
    22     3    4  0.424247
    23     3    5  0.586254
    24     4    0  0.317644
    25     4    1  0.000000
    26     4    2  0.000000
    27     4    3  0.271171
    28     4    4  0.586686
    29     4    5  0.424560
    

    【讨论】:

      猜你喜欢
      • 2022-06-22
      • 2023-02-02
      • 2018-08-21
      • 2017-02-09
      • 2020-11-09
      • 2023-04-07
      • 2018-02-08
      • 2021-12-29
      • 1970-01-01
      相关资源
      最近更新 更多