如何遍历行,将值存储在向量中,并使用向量计算新列?

2024-04-24 10:12:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我的数据帧df,如下所示:

          Column    

0     0 [
        { “weight": “40", “height": 4,”age”:13 },
        { “weight": “50", “height": 10,”age”:15 },
        { “weight": “30", “height": 5,”age”:25 },
        { “weight": “25", “height”:5,”age”:35 }
        ]

1     1 [
        { “weight": “60", “height": 6, “age":45 },
        { “weight": “80", “height": 8, “age”:30 },
        { “weight": “90", “height": 9, “age”:20 },
        { “weight": “70", “height": 7, “age”:50 }
        ]

输出:

        weight            height              New_column (compute Weight/Height )
0     (40,50,30,25)     (4,10,5,5)             (10,5,6,5)
1     (60,80,90,70)     (6,8,9,7)             (10,10,10,10)

有人能为这个写一个伪代码或算法吗?我想在熊猫身上做这个。我想不出一个办法。你知道吗


Tags: 数据代码算法dfnewagecolumncompute
1条回答
网友
1楼 · 发布于 2024-04-24 10:12:06

您可以以宽格式保存数据,并且仍然可以获得所需的weight:height比率:

orig
                                             Columns
0  [{'weight': '40', 'height': 4, 'age': 13}, {'w...
1  [{'weight': '60', 'height': 6, 'age': 45}, {'w...

def extract(row, field):
    return [int(x[field]) for x in row.Columns]

df = orig.assign(weight=orig.apply(extract, args=("weight",), axis=1).values,
                 height=orig.apply(extract, args=("height",), axis=1).values)

df['ratio'] = df.apply(lambda x: pd.Series(x.weight)/pd.Series(x.height), 
                       axis=1).values.tolist()

df
          height            weight                     ratio
0  [4, 10, 5, 5]  [40, 50, 30, 25]     [10.0, 5.0, 6.0, 5.0]
1   [6, 8, 9, 7]  [60, 80, 90, 70]  [10.0, 10.0, 10.0, 10.0]

相关问题 更多 >