【问题标题】:Correlating between two sheets of excel using Python使用 Python 在两张 excel 之间进行关联
【发布时间】:2020-07-20 20:04:29
【问题描述】:

如果 sheet1 的值与 sheet2 匹配,我正在尝试从 sheet2 获取数据。

例如:

工作表1:

name    school
Rahul   DPS
Seema   KV

表2:

school_name profile       color       subject
DPS           1      orange,blue        math
DPS           2      red,purple        english
KV            1        yellow          science

如果在 sheet1 school 中与 sheet2 school_name 匹配,则取其所有数据,即。个人资料、颜色、主题。

例如。 sheet1 DPS 与 sheet2 DPS 匹配比输出如下:

name= Rahul
school:{DPS:[{'profile':1,'color':orange,blue,'subject':math},{'profile':2,'color':red,purple,'subject':english}]}

【问题讨论】:

    标签: python-3.x excel pandas list dictionary


    【解决方案1】:

    由于您没有告诉我们您的 excel 列名的规则,我假设它们是非结构化的并且可以更改,

    要获得这两个数据帧之间的匹配,我们可以使用一系列stack() 和布尔索引

    如果这是您的第二个数据框;

    print(df2)
    
      school_name  profile        color  subject
    0         DPS        1  orange,blue     math
    1         DPS        2   red,purple  english
    2          KV        1       yellow  science
    3         RDM        2          red  history
    
    
    s = df2.stack().isin(df1.stack()).unstack()
    df3 = df2.loc[s[s.eq(True)].dropna(how='all').index]
    
    print(df3)
    
      school_name  profile        color  subject
    0         DPS        1  orange,blue     math
    1         DPS        2   red,purple  english
    2          KV        1       yellow  science
    

    然后您可以运行您的 groupby 操作。

    【讨论】:

      猜你喜欢
      • 2012-03-20
      • 1970-01-01
      • 1970-01-01
      • 2017-12-16
      • 2018-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多