简体   繁体   English

Pandas:将两列值与嵌套字典进行比较

[英]Pandas: compare two columns values against a nested dictionary

I was reading a lot but I couldn't figure out how can I do this.我读了很多书,但我不知道该怎么做。 I have a df with two columnns: data and fold , and a nested dictionary called dicDataFolds with data as keys and fold as key of each dictionary like this:我有一个带有两个列的 df : datafold ,以及一个名为dicDataFolds的嵌套字典,其中data作为键, fold作为每个字典的键,如下所示:

dicDataFolds =  {'data1': {1: 111936, 2: 111928, 3: 111821, 4: 111852, 5: 111882},
                 'data2': {1: 1566460, 2: 1566434, 3: 1566245, 4: 1566502, 5: 1566550},
                 'data3': {1: 292478, 2: 316757, 3: 316671, 4: 292265, 5: 316018},
                  ...
                  }

I would like to access to the dictionary in order to obtain the value pointed by the 'data' and 'fold' columns values, and store it in a new column.我想访问字典以获得“数据”和“折叠”列值指向的值,并将其存储在新列中。

Any suggestions?有什么建议么?

  • Write a function that写一个 function ,
    • takes a Series as an argument;将 Series 作为参数;
    • extracts the data and fold values from that Series;从该系列中提取datafold值;
    • accesses the dictionary with those values;使用这些值访问字典;
    • and returns the result.并返回结果。
  • apply the function along axis 1 of dataframe[['data','fold']]沿数据帧的轴 1 应用 function dataframe[['data','fold']]
  • assign the return value from that application to a new column of the DataFrame将该应用程序的返回值分配给 DataFrame 的新列

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM