简体   繁体   English

使用来自同一行但不同列的值填充字典

[英]Fill dictionary with value from the same row, but different column

Lately I've been trying to map some values, so I'm trying to create a dictionary to do so. 最近我一直试图映射一些值,所以我试图创建一个字典来做到这一点。 The odd thing is my DataFrame has a column made of lists, and DataFrames are always a bit awkward with lists. 奇怪的是我的DataFrame有一个由列组成的列,而DataFrame对列表总是有点尴尬。 The DataFrame has the following structure: DataFrame具有以下结构:

    rules          procedure
['10','11','12']       1
['13','14']            2
['20','21','22','24']  3

So I want to create a dictionary that maps '10' to 1, '14' to 2, and so on. 所以我想创建一个字典,将'10'映射到1,'14'映射到2,依此类推。 I tried the following: 我尝试了以下方法:

dicc=dict()
for j in df['rules']:
    for i,k in zip(j,df.procedure):
        dicc[i]=k

But that isn't making it. 但那不是成功的。 Probably something to do with indexes. 可能与索引有关。 What am I missing? 我错过了什么?

Edit: I'm trying to create a dictionary that maps the values '10', '11', '12' to 1; 编辑:我正在尝试创建一个将值'10','11','12'映射到1的字典; '13','14' to 2; '13','14'到2; '20','21','22','24' to 3, so if I type dicc['10'] I get 1 , if I type dicc['22'] I get 3 . “20”,“21”,“22”,“24”到3,所以如果我输入dicc['10']我得到1 ,如果我输入dicc['22']我得到3 Obviously, the actual DataFrame is quite bigger and I can't do it manually. 显然,实际的DataFrame非常大,我无法手动完成。

You can do it like this: 你可以这样做:

import pandas as pd

data = [[['10', '11', '12'], 1],
        [['13', '14'], 2],
        [['20', '21', '22', '24'], 3]]

df = pd.DataFrame(data=data, columns=['rules', 'procedure'])

d = {r : p for rs, p in df[['rules', 'procedure']].values for r in rs}
print(d)

Output 产量

{'20': 3, '10': 1, '11': 1, '24': 3, '14': 2, '22': 3, '13': 2, '12': 1, '21': 3}

Notes: 笔记:

  • The code {r : p for rs, p in df[['rules', 'procedure']].values for r in rs} is a dictionary comprehension, the dictionary counterpart of list. 代码{r : p for rs, p in df[['rules', 'procedure']].values for r in rs}是字典理解,是列表的字典对应物。
  • The df[['rules', 'procedure']].values is equivalent to zip(df.rules, df.procedure) it outputs a pair of list, int. df[['rules', 'procedure']].values相当于zip(df.rules, df.procedure)它输出一对list,int。 So the rs variable is a list and p is an integer. 所以rs变量是一个列表, p是一个整数。
  • Finally you iterate over the values of rs using the second for loop 最后,使用第二个for循环迭代rs的值

UPDATE UPDATE

As suggested for @piRSquared you can use zip: 正如@piRSquared的建议你可以使用zip:

d = {r : p for rs, p in zip(df.rules, df.procedure) for r in rs}

Help from cytoolz 来自cytoolz帮助

from cytoolz.dicttoolz import merge

merge(*map(dict.fromkeys, df.rules, df.procedure))

{'10': 1,
 '11': 1,
 '12': 1,
 '13': 2,
 '14': 2,
 '20': 3,
 '21': 3,
 '22': 3,
 '24': 3}

Note 注意

I updated my post to mimic how @jpp passed multiple iterables to map . 我更新了我的帖子以模仿@jpp如何通过多个迭代来map @jpp's answer is very good . @jpp的答案非常好 Though I'd advocate for upvoting all useful answers, I wish I could upvote their answer again (-: 虽然我主张提出所有有用的答案,但我希望我能再次提出他们的答案( - :

Using collections.ChainMap : 使用collections.ChainMap

from collections import ChainMap

res = dict(ChainMap(*map(dict.fromkeys, df['rules'], df['procedure'])))

print(res)

{'10': 1, '11': 1, '12': 1, '13': 2, '14': 2,
 '20': 3, '21': 3, '22': 3, '24': 3}

For many uses, the final dict conversion is not necessary: 对于许多用途,最终的dict转换不是必需的:

A ChainMap class is provided for quickly linking a number of mappings so they can be treated as a single unit. 提供了一个ChainMap类,用于快速链接多个映射,以便将它们视为一个单元。 It is often much faster than creating a new dictionary and running multiple update() calls. 它通常比创建新字典和运行多个update()调用快得多。

See also What is the purpose of collections.ChainMap? 另请参见collections.ChainMap的目的是什么?

You may check flatten the list 您可以检查展平列表

dict(zip(sum(df.rules.tolist(),[]),df.procedure.repeat(df.rules.str.len())))
Out[60]: 
{'10': 1,
 '11': 1,
 '12': 1,
 '13': 2,
 '14': 2,
 '20': 3,
 '21': 3,
 '22': 3,
 '24': 3}

using itertools.chain and DataFrame.itertuples : 使用itertools.chainDataFrame.itertuples

dict(
    chain.from_iterable(
        ((rule, row.procedure) for rule in row.rules) for row in df.itertuples()
    )
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pandas:同列不同行如何填写值 - Pandas:How to fill in value from the same column but different row 根据同一行中另一列的值填充缺失值 - Fill missing value based on value from another column in the same row 根据Pandas中第二列的条件,用另一行的同一列的值填充特定行的列中的值 - Fill values in a column of a particular row with the value of same column from another row based on a condition on second column in Pandas 使用pandas在csv文件的同一行上填充下一列值的行中的空值 - Fill empty values from a row with the value of next column on the same row on csv file with pandas Python/Pandas:如果值为 NaN 或 0,则用同一行内下一列的值填充 - Python/Pandas: if value is NaN or 0 then fill with the value from the next column within the same row 使用 Python Pandas 将列值与不同列进行比较,并从同一行但不同列返回值 - Compare a Column value to different columns and return a value from same row but different column using Python Pandas 用相同的值填充列,但组中的第一行除外 - Fill column with same value except first row inside groups 如何遍历 Pandas DF 中的列以检查某个值并返回同一行但来自不同列的值? - How to iterate over a column in a Pandas DF to check for a certain value and return a value in the same row but from a different column? 使用 Python 和 pandas 在列中搜索值并返回同一行但不同列的值 - Search for a value in a column and return value from the same row but different column using Python and pandas Pandas 用行值填充列 - Pandas fill column with row value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM