python - 更快地迭代 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))
解决方案
推荐阅读
- mqtt - 当我使用带有红色节点的 MQTT 5.0 时,是否需要任何特殊配置?
- macos - 通过 macOS SwiftUI 应用程序中的应用程序菜单访问前窗口内容?
- tensorflow - TensorFlow中的多元插值方法?
- c# - 如何从资源 ID 获取 blob 属性
- django - 为什么 DRF 建议覆盖序列化程序中的操作?
- groovy - 从 groovy 对象列表中删除属性的最佳方法
- java - 设备培训:无法创建解释器:Op builtin_code 超出范围:142。您是否使用较新型号的旧 TFLite 二进制文件?
- node.js - 运行应用程序时面临 Gulp 问题
- python - 为什么 jupyter 中的 pd.read_excel 停止工作并显示此错误消息?
- javascript - 反应 SSR。调用 ReactDOM.hydrate() 的正确位置