首页 > 解决方案 > 更快地迭代 Dataframe 的方法

问题描述

我有一个数据框,其中一列是列表。我想每次从该列表中提取一个值,并将基于该值的新行附加到新的数据帧中。

东风:

       0      1      2
0   'Abcd'   5623   ['one', 'two']
1   'Brdd'   5624   ['three']
2   'Vbcd'   4223   ['five', 'six', 'seven']
3   'Mkln'   5873   []

结果:

       0      1      2
0   'Abcd'   5623   'one'
1   'Abcd'   5623   'two'
2   'Brdd'   5624   'three'
3   'Vbcd'   4223   'five'
4   'Vbcd'   4223   'six'
5   'Vbcd'   4223   'seven'

我想出了下面的功能,但它超级慢。我想知道在熊猫中是否有更好的方法来做到这一点。

for index, row in df.iterrows():
    for el in df['Column']:
        temp = df.iloc[index]
        temp['Column'] = el
        df_clear = df_clear.append(temp)
    print("Currently on row: {}; Currently iterated {}% of rows".format(index, (index + 1) / len(df.index) * 100))

标签: pythonpandas

解决方案


推荐阅读