Python 在循环中将字典附加到 Pandas 数据帧

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43957347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:34:00  来源:igfitidea点击:

Append dictionary to pandas dataframe in a loop

pythonpython-3.xpandas

提问by Leo

I have a requirement to create a dictionary within a loop and append them to a pandas data frame with matching key name of dict and column name of data frame. The key value pairs of dictionary in each iteration could be different. An empty pandas data frame df_podcasthave been defined at the beginning with all possible keys in the dictionary.

我需要在循环中创建一个字典,并将它们附加到一个 Pandas 数据框中,该数据框具有匹配的 dict 键名和数据框的列名。每次迭代中字典的键值对可能不同。一个空的 Pandas 数据框df_podcast已经在开始时定义了字典中所有可能的键。

Below is the sample of a code which is not completed yet

以下是尚未完成的代码示例

df_podcast=pd.DataFrame(columns=podcast_cols)

podcast_dict={}
for j in range(len(podcast[0])):
    if podcast[0][j].tag=="key":
        podcast_dict[podcast[0][j].text]=podcast[0][j+1].text
### Have to append dict to pandas df ############

I have append podcast_dict to df_podcast. Podcast is actually a list of lists, here I'm just considering only 1st row of the list

我已将 podcast_dict 附加到 df_podcast。播客实际上是一个列表列表,这里我只考虑列表的第一行

回答by Sumanth Lazarus

If you want to simply append new data from a created dictionary within a loop to an existening Dataframe:

如果您想简单地将循环内创建的字典中的新数据附加到现有数据帧:

df = pd.DataFrame()
for i in range(n):
    dict_new = dict(i)
    df = df.append(dict_new, ignore_index=True)
print(df)

NOTE:As long as the keys in your created dictionary are the same, appending it to an existing dataframe shouldn't be cumbersome. Source

注意:只要您创建的字典中的键相同,将其附加到现有数据帧就不会很麻烦。 来源

回答by xingpei Pang

You need:

你需要:

df  = pd.DataFrame([podcast_dict], columns=podcast_dict.keys())
df_podcast = pd.concat([df_podcast, df], axis =0).reset_index()

回答by Scott Boston

IIUC:

IUC:

What you need to do is to build your dictionary with your loop, then at then end of your loop, you can use your dictionary to create a dataframe with:

你需要做的是用你的循环构建你的字典,然后在你的循环结束时,你可以使用你的字典来创建一个数据框:

df1  = pd.DataFrame(podcast_dict)

And append using pd.concat:

并附加使用pd.concat

df_podcast = pd.concat([df_podcast, df1])