我还在循环中使用了数据框的 append 函数,感到困惑的是它的运行速度如何。
根据此页面上的正确答案,为遭受苦难的人提供有用的示例。
Python版本:3
熊猫版:0.20.3
# the dictionary to pass to pandas dataframedict = {}# a counter to use to add entries to "dict"i = 0# Example data to loop and append to a dataframedata = [{"foo": "foo_val_1", "bar": "bar_val_1"}, {"foo": "foo_val_2", "bar": "bar_val_2"}]# the loopfor 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 rowsdf = Dataframe.from_dict(dict, "index")“ from_dict”函数:https
://pandas.pydata.org/pandas-
docs/stable/generation/pandas.Dataframe.from_dict.html



