简体   繁体   English

如何在不使用 python 循环的情况下创建引用数据框和字典的当前列的条件列?

[英]How can i create conditional column referring present columns of a dataframe and dictionary without using loop in python?

I have a datafarme我有一个数据农场

import pandas as pd

df = pd.DataFrame({"type":  ["A" ,"A1" ,"A" ,"A1","B" ],
                  "group":  ["g1", "g2","g2","g2","g1"]})

And i have a dictionary我有一本字典

 dic ={"AlphaA": {"A":  {"g1":"A_GRP1",  "g2":"A_GRP2"},
                  "A1": {"g1":"A1_GRP1", "g2":"A1_GRP2"}},
       "AlphaB": {"B":  {"g1":"B_GRP1",  "g2":"B_GRP2"}},
      }

i have to create a column name "value", which will use the data frame and dictionary and get value assigned to it我必须创建一个列名“值”,它将使用数据框和字典并获取分配给它的值

Conditions to be applied:申请条件:

  1. if type is "A" or "A1" it should refer dictionary key AlphaA and get the value for respective group and assign it to new column如果类型是“A”或“A1”,它应该引用字典键 AlphaA 并获取相应组的值并将其分配给新列
  2. if type is "B", it should refer dictionary key AlphaB and get the value of the respective group如果类型是“B”,它应该引用字典键 AlphaB 并获取相应组的值

Example of row one:第一行示例:
type is "A" hence refering dictionary key "AlphaA"类型是“A”,因此引用字典键“AlphaA”
group is "g1组是“g1
therefore :因此

dictt["AlphaA"]["A"]["g1"]          #would be the answer  

Required Output所需输出

 final_df = pd.DataFrame({"type" :  ["A" ,"A1" ,"A" ,"A1","B" ],
                          "group":  ["g1", "g2","g2","g2","g1"],
                          "value":  ["A_GRP1", "A1_GRP2", "A_GRP2",
                                     "A1_GRP2", "B_GRP1"]})

I was able to achieve this using loops but its is taking lot of time,我能够使用循环来实现这一点,但它需要很多时间,
hence looking for some speedy technique.因此寻找一些快速的技术。

Assuming dic the input dictionary, you can merge the dictionary values into a single dictionary (with help of ChainMap ), convert to DataFrame and unstack to Series and merge :假设dic输入字典,您可以将字典值合并到单个字典中(在ChainMap的帮助下),转换为 DataFrame 并取消unstack到 Series 并merge

from collections import ChainMap
s = pd.DataFrame(dict(ChainMap(*dic.values()))).unstack()

# without ChainMap
# d = {k: v for d in dic.values() for k,v in d.items()}
# pd.DataFrame(d).unstack()

out = df.merge(s.rename('value'), left_on=['type', 'group'], right_index=True)

output:输出:

  type group    value
0    A    g1   A_GRP1
1   A1    g2  A1_GRP2
3   A1    g2  A1_GRP2
2    A    g2   A_GRP2
4    B    g1   B_GRP1

Use DataFrame.join with Series created from dictionary by dict comprehension:DataFrame.join与通过字典理解从字典创建的 Series 一起使用:

d1 = {(k1, k2): v2 for k, v in d.items() for k1, v1 in v.items() for k2, v2 in v1.items()}
df = df.join(pd.Series(d1).rename('value'), on=['type','group'])
print (df)
  type group    value
0    A    g1   A_GRP1
1   A1    g2  A1_GRP2
2    A    g2   A_GRP2
3   A1    g2  A1_GRP2
4    B    g1   B_GRP1

You can remove the outer key of original dictionary and try apply on rows您可以删除原始字典的外键并尝试应用于行

d = {k:v for vs in d.values() for k, v in vs.items()}
df['value'] = (df.assign(value=df['type'].map(d))
               .apply(lambda row: row['value'][row['group']], axis=1)
               )
print(d)

{'A': {'g1': 'A_GRP1', 'g2': 'A_GRP2'}, 'A1': {'g1': 'A1_GRP1', 'g2': 'A1_GRP2'}, 'B': {'g1': 'B_GRP1', 'g2': 'B_GRP2'}}

print(df)

  type group    value
0    A    g1   A_GRP1
1   A1    g2  A1_GRP2
2    A    g2   A_GRP2
3   A1    g2  A1_GRP2
4    B    g1   B_GRP1

暂无
暂无

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

相关问题 如何在另一个数据帧 python pandas 中的多列上使用条件逻辑在数据帧中创建一列? - How can I create a column in a dataframe using conditional logic on multiple columns in another dataframe python pandas? 如何使用for循环在pandas数据框中的现有列上创建条件列 - How to create new column conditional on existing columns in pandas dataframe using for loop 如何在 Python 中使用 for 循环创建嵌套字典? - How can I create a nested dictionary using a for loop in Python? Python 如何使用多个 pandas dataframe 列中的值作为元组键和单个列作为值来创建字典 - Python how to create a dictionary using the values in multiple pandas dataframe columns as tuple keys and a single column as value 在 pandas dataframe 中,如何在不使用循环的情况下根据一列的数据设置其他列的值? - In a pandas dataframe, how can I set the value of other columns based on the data from one column, without using a loop? 如何将 Pandas DataFrame 转换为只有值而没有列的 Python 字典? - How can I convert a pandas DataFrame to Python Dictionary with only values and without columns? 如何在不指定熊猫的列名的情况下从数据框创建字典? - How can i create a dictionary from a dataframe without specifying column names in pandas? 如何从我的 python 代码中创建单个 Dataframe 代码,为 for 循环的每次迭代生成字典? - How can I create a single Dataframe out of my python code that generates a dictionary for each iteration of a for loop? 如何将 Pandas 数据框转换为没有列标题的字典? - How can I transform a pandas dataframe into a dictionary without the column headers? 如何从同一个 dataframe 中的字典键创建列? - How can I create column from dictionary keys in same dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM