简体   繁体   English

从多行加入 Pandas 中的 json 文件

[英]Join json files in Pandas from multiple rows

I am given a data frame (Table 1) with the following format.我得到了一个具有以下格式的数据框(表 1)。 It has only col1 and col2, and json_col.它只有 col1 和 col2,以及 json_col。

id    col1   col2   json_col
 1     a      b        json1
 2     a      c        json2
 3     b      d        json3
 4     c      a        json4
 5     d      e        json5

I have a new table (Table 2) and I would like to join json files in my new table我有一个新表(表 2),我想在我的新表中加入 json 文件

col1   col2   col3  col4  union_json
a      b                 json1
a      b      d          json1 and json3 union
a      b      d     e    json1, json3, and json5 union 
c      a                 json4

Here is an example of Table 1以下是表 1 的示例

df = pd.DataFrame({'col1': ['a', 'a', 'b', 'c', 'd'],
                  'col2': ['b', 'c', 'd', 'a', 'e'],
                  'col3': [{"origin":"a","destination":"b", "arc":[{"Type":"763","Number":"20"}]},
                        {"origin":"a","destination":"c", "arc":[{"Type":"763","Number":"50"}]},
                        {"origin":"a","destination":"d", "arc":[{"Type":"723","Number":"40"}]},
                        {"origin":"c","destination":"a", "arc":[{"Type":"700","Number":"30"}]},
                        {"origin":"d","destination":"e", "arc":[{"Type":"700","Number":"40"}]}]})

And, here is an example of Table 2:并且,这是表 2 的示例:

df = pd.DataFrame({'col1': ['a', 'a', 'a', 'c'],
                  'col2': ['b', 'b', 'b', 'a'],
                  'col3': ['', 'd', 'd', ''],
                  'col4': ['', '', 'e', '']})

The union of json1 and json2 should look like this: json1 和 json2 的联合应该是这样的:

 [[{"origin":"a","destination":"b", "arc":[{"Type":"763","Number":"20"}]}], 
 [{"origin":"a","destination":"d", "arc":[{"Type":"723","Number":"40"}]}]]

I hope I've understood your question right:我希望我正确理解了您的问题:

from itertools import combinations


def fn(x):
    out, non_empty_vals = [], x[x != ""]

    for c in combinations(non_empty_vals, 2):
        out.extend(df1.loc[df1[["col1", "col2"]].eq(c).all(axis=1), "col3"])

    return out


df2["union_json"] = df2.apply(fn, axis=1)
print(df2.to_markdown(index=False))

Prints:印刷:

col1 col1 col2 col2 col3 col3 col4 col4 union_json union_json
a一个 b b [{'origin': 'a', 'destination': 'b', 'arc': [{'Type': '763', 'Number': '20'}]}] [{'origin': 'a', 'destination': 'b', 'arc': [{'Type': '763', 'Number': '20'}]}]
a一个 b b d d [{'origin': 'a', 'destination': 'b', 'arc': [{'Type': '763', 'Number': '20'}]}, {'origin': 'a', 'destination': 'd', 'arc': [{'Type': '723', 'Number': '40'}]}] [{'origin': 'a', 'destination': 'b', 'arc': [{'Type': '763', 'Number': '20'}]}, {'origin': 'a ', '目的地': 'd', 'arc': [{'Type': '723', 'Number': '40'}]}]
a一个 b b d d e e [{'origin': 'a', 'destination': 'b', 'arc': [{'Type': '763', 'Number': '20'}]}, {'origin': 'a', 'destination': 'd', 'arc': [{'Type': '723', 'Number': '40'}]}, {'origin': 'd', 'destination': 'e', 'arc': [{'Type': '700', 'Number': '40'}]}] [{'origin': 'a', 'destination': 'b', 'arc': [{'Type': '763', 'Number': '20'}]}, {'origin': 'a ', 'destination': 'd', 'arc': [{'Type': '723', 'Number': '40'}]}, {'origin': 'd', 'destination': 'e ', 'arc': [{'Type': '700', 'Number': '40'}]}]
c c a一个 [{'origin': 'c', 'destination': 'a', 'arc': [{'Type': '700', 'Number': '30'}]}] [{'origin': 'c', 'destination': 'a', 'arc': [{'Type': '700', 'Number': '30'}]}]

Dataframes used:使用的数据框:

df1

  col1 col2                                                                           col3
0    a    b  {'origin': 'a', 'destination': 'b', 'arc': [{'Type': '763', 'Number': '20'}]}
1    a    c  {'origin': 'a', 'destination': 'c', 'arc': [{'Type': '763', 'Number': '50'}]}
2    b    d  {'origin': 'a', 'destination': 'd', 'arc': [{'Type': '723', 'Number': '40'}]}
3    c    a  {'origin': 'c', 'destination': 'a', 'arc': [{'Type': '700', 'Number': '30'}]}
4    d    e  {'origin': 'd', 'destination': 'e', 'arc': [{'Type': '700', 'Number': '40'}]}

df2

  col1 col2 col3 col4
0    a    b          
1    a    b    d     
2    a    b    d    e
3    c    a           

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

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