【问题标题】:Exploding a data frame row by row and storing the exploded values in a new dataframe逐行分解数据框并将分解后的值存储在新数据框中
【发布时间】:2022-12-02 21:18:27
【问题描述】:

我有以下代码。

我想浏览“异常值数据框”数据框逐行爆炸价值在“x”和“y”列中。

对于每个分解的行,我想将这个分解的行存储为它自己的数据框, 和“newID”、“x”和“y”列.

但是,以下代码打印一栏中的所有内容而不是打印一列中的分解“x”值, 这分解另一列中的“y”值

我将非常感谢您的帮助!

individualframe = outlierdataframe.iloc[0]
individualoutliers = individualframe.explode(list('xy'))
newframe = pd.DataFrame(individualoutliers)
print(newframe)

离群值数据框第一行:

索引离群值数据帧的第一行:

outlierdataframe.iloc[0]

index                                                      24
subID                                         Prolific_610020
level                                                       1
complete                                                False
duration                                            20.015686
map_view                                            12.299759
distance                                           203.426697
x           [55, 55, 55, 60, 60, 60, 65, 70, 70, 75, 80, 8...
y           [60, 60, 60, 60, 65, 65, 70, 70, 75, 75, 80, 8...
r           [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 1...
batch                                                       1
newID                                                  610020
Name: 24, dtype: object

newframe = pd.DataFrame(individualoutliers)
print(newframe)

                24
0                 24
1    Prolific_610020
2                  1
3              False
4          20.015686
..               ...
121               55
122               55
123               55
124                1
125           610020

【问题讨论】:

    标签: python pandas dataframe numpy indexing


    【解决方案1】:

    您可以使用 pandas.DataFrame.applypandas.Series.explode 来展开您选择的 (list) 列(例如,xy)。

    尝试这个 :

    out = (
            df
              .loc[:, ["newID", "x", "y"]]
              .apply(lambda x: pd.Series(x).explode())
          )
    

    # 输出 :

    print(out)
    
        newID    x    y
    0  610020  100   60
    0  610020   55   60
    0  610020   55   60
    0  610020   60   60
    0  610020   60   65
    0  610020   60   65
    0  610020   65   70
    0  610020   70   70
    0  610020   70   75
    0  610020   75   75
    0  610020   80   80
    

    如果您需要为每个组分配一个数据帧(带有模式名称df_newID),请使用:

    for k, g in out.groupby("newID"):
        globals()['df_' + str(k)] = g
        
    print(df_610020, type(df_610020))
    
        newID    x    y
    0  610020  100   60
    0  610020   55   60
    0  610020   55   60
    0  610020   60   60
    0  610020   60   65
    0  610020   60   65
    0  610020   65   70
    0  610020   70   70
    0  610020   70   75
    0  610020   75   75
    0  610020   80   80 <class 'pandas.core.frame.DataFrame'>
    

    【讨论】:

    • 非常感谢 - 这也有效! :) 'globals()' 需要什么?
    【解决方案2】:

    以下解决方案有效:

    individualframe = outlierdataframe.iloc[0]
    individualoutliers1 = individualframe[['x']].explode('x')
    individualoutliers2 = individualframe[['y']].explode('y')
    newIDs = individualframe[['newID']][0]
    individualoutliers1 = pd.DataFrame(individualoutliers1)
    individualoutliers2 = pd.DataFrame(individualoutliers2)
    data = [individualoutliers1,individualoutliers2]
    newframe = pd.concat(data,axis=1)
    newframe = newframe.rename(columns={newframe.columns.values[0]:'x',newframe.columns.values[1]:'y'})
    newframe['newID'] = newIDs 
    print(newframe)
    
    
    Output exceeds the size limit. Open the full output data in a text editor
          y    y   newID
    0    55   60  610020
    1    55   60  610020
    2    55   60  610020
    3    60   60  610020
    4    60   65  610020
    5    60   65  610020
    6    65   70  610020
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-17
      • 1970-01-01
      • 2021-07-28
      • 1970-01-01
      • 1970-01-01
      • 2017-02-12
      相关资源
      最近更新 更多