您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

改善Pandas DataFrame的行追加性能

改善Pandas DataFrame的行追加性能

我还在循环中使用了数据框的 函数,感到困惑的是它的运行速度如何。

根据此页面上的正确答案,为遭受苦难的人提供有用的示例。

Python版本:3

熊猫版:0.20.3

# the dictionary to pass to pandas dataframe
dict = {}

# a counter to use to add entries to "dict"
i = 0

# Example data to loop and append to a dataframe
data = [{"foo": "foo_val_1", "bar": "bar_val_1"}, 
       {"foo": "foo_val_2", "bar": "bar_val_2"}]

# the loop
for entry in data:

    # add a dictionary entry to the final dictionary
    dict[i] = {"col_1_title": entry['foo'], "col_2_title": entry['bar']}

    # increment the counter
    i = i + 1

# create the dataframe using 'from_dict'
# important to set the 'orient' parameter to "index" to make the keys as rows
df = DataFrame.from_dict(dict, "index")

“ from_dict”函数https://pandas.pydata.org/pandas- docs/stable/generation/pandas.DataFrame.from_dict.html

其他 2022/1/1 18:43:44 有408人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶