pandas 用循环附加到字典

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50973663/
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-09-14 05:43:34  来源:igfitidea点击:

Appending to dictionary with loop

pythonpandasdictionaryfor-loop

提问by florence-y

I want to create a dictionary with a predetermined list, however, I can't seem to figure it out how to avoid overwriting instead of appending, and I'm not sure if I can avoid importing any other modules.

我想创建一个带有预定列表的字典,但是,我似乎无法弄清楚如何避免覆盖而不是附加,而且我不确定是否可以避免导入任何其他模块。

The scope is that I have a data frame of 1 column of character names with ID numbers attached to the names from reading an excel file in, sega_df:

范围是我有一个包含 1 列字符名称的数据框,其中 ID 号附加到读取 excel 文件的名称中sega_df

          Character
0         Amy (335)
1       Tails (359)
2      Shadow (357)
3      Shadow (357)
4       Blaze (337)

Then I have a list of all the characters, characters, without their ID numbers:

然后我有一个所有字符的列表,characters,没有他们的 ID 号:

['Sonic', 'Knuckles', 'Tails', 'Amy', 'Cream', 'Shadow', 'Rouge', 'Silver', 'Blaze']

I want to create a dictionary so I can replace sega_df.Character's by slicing each row entry with the len()of the characters in characters, producing desired_sega_df:

我想创建一个字典,这样我就可以sega_df.Character通过用len()中字符的 切片来替换' characters,产生desired_sega_df

         Character
    0          Amy
    1        Tails
    2       Shadow
    3       Shadow
    4        Blaze

The dictionary I want to create will have keys of the characters names without their ID numbers, and values of the len()of their names. The dictionary is slice:

我要创建的字典将包含字符名称的键,而不包含它们的 ID 号,以及len()它们名称的值。字典是slice

{'Sonic': 5, 
 'Knuckles': 8, 
 'Tails': 5, 
 'Amy': 3, 
 'Cream': 5, 
 'Shadow': 6, 
 'Rouge': 5, 
 'Silver': 6, 
 'Blaze': 5}

Even when I use .update()it still repeatedly overwrites with only Blazeas the key and 5as the value.

即使我使用.update()它仍然重复覆盖仅Blaze作为键和5值。

>>> for character in characters:
...     slice = {character: len(character)}
...     slice.update({character:len(character)})
...
>>> slice
{'Blaze': 5}

My question is: How can I modify my loop to add key-value pairs of all the characters to slicerather than continuously overwriting them?

我的问题是:如何修改循环以将所有字符的键值对添加到其中slice而不是不断覆盖它们?

回答by Venkata Gogu

Update your code to :

将您的代码更新为:

>>> slice = dict()
>>> for character in characters:
...     slice.update({character:len(character)})
...

回答by jpp

Here's the Pandorable solution. For splitting Character, you have a choice of splitting on whitespace or slicing on character count. Which works best depends on your dataset.

这是 Pandorable 解决方案。对于 splitting Character,您可以选择在空白处进行拆分或按字符数进行切片。哪种效果最好取决于您的数据集。

Whether you choose the pure Python or Pandas solution, you do not need to use an explicit loop.

无论您选择纯 Python 还是 Pandas 解决方案,都不需要使用显式循环。

# remove last 6 characters to leave names
df['Character'] = df['Character'].str[:-6]  # or, df['Chracter'].str.split().str[0]

# calculate length in new series
df['Length'] = df['Character'].map(len)

# convert to dictionary
d = df.set_index('Character')['Length'].to_dict()

print(d)

{'Amy': 3, 'Tails': 5, 'Shadow': 6, 'Blaze': 5}

回答by Benjamin Engwall

You should define sliceas an empty dictionary outside of your loop. As it currently stands, you redefine the dictionary for each character as you iterate.

您应该slice在循环之外定义为空字典。按照目前的情况,您在迭代时为每个字符重新定义字典。