代码之家  ›  专栏  ›  技术社区  ›  Florent

如何用熊猫分割两个不同形状的数据帧?

  •  1
  • Florent  · 技术社区  · 6 年前

    我有两个索引相同但形状不同的数据帧,无法将列与数据帧分开 df1 数据框中的列 df2 是的。

    预期结果是 df1 / df2 是的。

    df1.head()
                               volume  volume        volume         volume  \
    timestamp                                                                
    2016-07-24 00:00:00+00:00     NaN     NaN           NaN            NaN   
    2016-07-25 00:00:00+00:00     NaN     NaN           NaN            NaN   
    2016-07-26 00:00:00+00:00     NaN     NaN           NaN  102720.829507   
    2016-07-27 00:00:00+00:00     NaN     NaN  3.729644e+05  398346.509801   
    2016-07-28 00:00:00+00:00     NaN     NaN  1.326648e+06  244165.794698   
    
                               volume        volume  volume        volume  
    timestamp                                                              
    2016-07-24 00:00:00+00:00     NaN           NaN     NaN  1.734943e+07  
    2016-07-25 00:00:00+00:00     NaN           NaN     NaN  1.365341e+07  
    2016-07-26 00:00:00+00:00     NaN           NaN     NaN  5.199938e+07  
    2016-07-27 00:00:00+00:00     NaN  2.471076e+06     NaN  2.558753e+07  
    2016-07-28 00:00:00+00:00     NaN  1.642990e+06     NaN  3.118785e+06
    
    df2.head()
    
    timestamp
    2016-07-24 00:00:00+00:00    1.734943e+07
    2016-07-25 00:00:00+00:00    1.365341e+07
    2016-07-26 00:00:00+00:00    5.210210e+07
    2016-07-27 00:00:00+00:00    2.882991e+07
    2016-07-28 00:00:00+00:00    6.332589e+06
    Freq: D, dtype: float64
    
    df1.shape
    Out[2126]: (723, 8)
    
    df2.shape
    Out[2127]: (723,)
    
    df1.divide(df2, axis= 'index')
    ValueError: operands could not be broadcast together with shapes (5784,) (723,) 
    

    两个数据帧的结构不同,但索引相同。

    type(df1)
    Out[2143]: pandas.core.frame.DataFrame
    
    type(df2)
    Out[2144]: pandas.core.series.Series
    

    我读到需要重塑其中一个数据帧,所以我尝试了如下操作:

    df1.divide(df2.reshape(723,1), axis= 'index')
    

    但它返回一个错误:

    ValueError: Unable to coerce to DataFrame, shape must be (723, 8): given (723, 1)
    

    当我皈依 DF2 具有 pd.DataFrame(df2) 然后它抛出一个错误:

    TypeError: '<' not supported between instances of 'str' and 'int' 
    

    我错过了什么,我该怎么做?

    2 回复  |  直到 6 年前
        1
  •  1
  •   Dtengu    6 年前

    试试这个方法我用了一个简单的例子,但如果不起作用,请告诉我。

    import pandas as pd
    import numpy as np
    from IPython.display import display, HTML
    
    CSS = """
    .output {
        flex-direction: row;
    }
    """
    
    HTML('<style>{}</style>'.format(CSS))
    
    
    data1 = {"a":[1.,7.,12.],
             "b":[4.,8.,3.],
             "c":[5.,45.,67.]}
    data2 = {"a":[3.],
             "b":[2.],
             "c":[8.]}
    
    df1 = pd.DataFrame(data1)
    df2 = pd.DataFrame(data2) 
    df2 = df2.T
    df2 = df2.reset_index()
    del df2['index']
    display(df1)
    display(df2)
    display(df1.iloc[:,0:].truediv(df2[0], axis=0)) # this portion of code you want
    


    甲、乙、丙
    0 1.0 4.0 5.0
    17.0 8.0 45.0条
    2 12.0 3.0 67.0条

    0个
    0 3.0分
    12.0条
    28.0条

    甲、乙、丙
    0.333333 1.333333 1.666667
    130万4.000000 22.500000
    2 150万0.375000 8.375000

        2
  •  1
  •   gau    6 年前

    在使用Delphi(或DIV)函数时,对每个数据框中的相应列/ S进行索引应该是有效的。

    df1[['column_1','column_2']].divide(df2[['column_1']], axis= 'index')  
    
    df1[['column_1','column_2']].div(df2[['column_1']], axis= 'index')