首页 > 解决方案 > how to convert one string row into many columns in pandas?

问题描述

I have a data frame with shape (30420279, 1). some rows contain simple values like size = 100, others include complex values like in the following snipping: enter image description here

other rows contain also other combination of values like the following snipping enter image description here

as we can see now, the rows are like dictionary formate {key = value}. what I wanted to do is to extend the data frame shape from one column (30420279, 1) up to many columns depending on the keys that we have like in the snipping (size, async, batchFrequency..).

标签: pythonpandas

解决方案


效率不是特别高,但给定参数列表,您可以使用相应的正则表达式生成列。

params = ['async', 'size']
for p in params:
    df[p] = df['long_string'].str.extract(fr'{p}=([^,]+)')

推荐阅读