【问题标题】:using one element from one datafram to find all the information in the other datafram? Python使用一个数据框中的一个元素来查找另一个数据框中的所有信息? Python
【发布时间】:2022-01-11 04:28:46
【问题描述】:

通过运行 2 个 sql 查询,我在 python 中有 2 个数据框。 表A:

Name Email Code
A E E1,E2,E3
B F F1,F2
C G G1
... ... ...

表 B:

Info1 Code Info2
... E1 ...
... F1 ...
... E2 ...
... E3 ...
... G1 ...
... F2 ...

因为它们是 Python 中的数据框,所以我想一一阅读电子邮件,并在 TableA 中找到它的代码列表/数组。 并使用列表/数组中的每个元素查找表 B 中的所有数据,创建 Excel 并发送到信息主机。 例如, 我想得到下面的excel结果并将其发送给E。

Info1 Code Info2
... E1 ...
... E2 ...
... E3 ...

例如, 我想得到下面的excel结果并将其发送给F。

Info1 Code Info2
... F1 ...
... F2 ...

例如, 我想得到下面的excel结果并将其发送给G。

Info1 Code Info2
... G1 ...

请在 Python 中使用数据框。 非常感谢

【问题讨论】:

    标签: python python-3.x pandas dataframe data-analysis


    【解决方案1】:

    你可以试试这样的:

    new_df = df1.assign(Code=df['Code'].str.split(',')).explode('Code').merge(df2)
    for email, sub_df in new_df.groupby(['Email']):
        print(f'Sending df to {email}...')
        print(sub_df.to_csv())
    

    输出:

    Sending df to E...
    ,Name,Email,Code,Info1,Info2
    0,A,E,E1,...,...
    1,A,E,E2,...,...
    2,A,E,E3,...,...
    
    Sending df to F...
    ,Name,Email,Code,Info1,Info2
    3,B,F,F1,...,...
    4,B,F,F2,...,...
    
    Sending df to G...
    ,Name,Email,Code,Info1,Info2
    5,C,G,G1,...,...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-09
      • 2021-10-29
      • 2018-04-19
      • 2023-04-08
      • 2021-09-19
      • 2021-04-22
      • 2017-11-18
      • 2017-01-20
      相关资源
      最近更新 更多