python - 如何处理一个 pandas 数据帧的行中的 ID 组并使用它们从另一个数据帧中提取记录

标签 python pandas dataframe

我有两个数据框。其中一份包含个人和家庭的联系信息。另一个包含家庭的 ID 字段,后面是该家庭中的个人。我想从第一个数据框中选择所有记录,并插入一列及其关联的家庭 ID。

最小可重复性:

df1 = pd.DataFrame({'Constituent Id':['111111','222222','333333','444444','555555','666666','777777'],
               'Type':['Individual','Household','Individual','Household',
                       'Individual','Individual','Individual'],
               'Name':['Panda Smith','Panda and Python','Python Jones','Postgres Family',
                       'Paul Postgres','Mary Postgres','Sqlite Postgres']})

df2 = pd.DataFrame({'Account_ID':['ABCDEF','GHIJKL'],
                    'Household_0':['222222','444444'],
                    'Individual_0':['111111','555555'],
                    'Individual_1':['333333','666666'],
                    'Individual_2':['','777777']})

结果:

 >>> df1
      Constituent Id        Type              Name
    0         111111  Individual       Panda Smith
    1         222222   Household  Panda and Python
    2         333333  Individual      Python Jones
    3         444444   Household   Postgres Family
    4         555555  Individual     Paul Postgres
    5         666666  Individual     Mary Postgres
    6         777777  Individual   Sqlite Postgres
>>> df2
      Account_ID Household_0 Individual_0 Individual_1 Individual_2
    0     ABCDEF      222222       111111       333333             
    1     GHIJKL      444444       555555       666666       777777

我想要做的是将一列附加到 df1 中,其中包含适用于帐户中每个人的 Account_ID。家庭不是必需的,但如果我把这些也包括在内就可以了。

由于每个家庭的人数各不相同,因此我想不出一种无需迭代每一行即可完成此操作的好方法。这看起来非常不像 Pandas ,我确信有更好的方法,也许是通过堆叠或其他方式。

在我的示例中,输出如下所示:

  Constituent Id        Type              Name   Account_ID  
0         111111  Individual       Panda Smith      ABCDEF
1         222222   Household  Panda and Python      ABCDEF
2         333333  Individual      Python Jones      ABCDEF
3         444444   Household   Postgres Family      GHIJKL
4         555555  Individual     Paul Postgres      GHIJKL
5         666666  Individual     Mary Postgres      GHIJKL
6         777777  Individual   Sqlite Postgres      GHIJKL

最佳答案

IIUC需要融化然后合并

如果。 Type 不是必需的,您可以从第二行和合并子句中省略它。

s = pd.melt(df2,id_vars='Account_ID',var_name='Type',value_name='Constituent Id')
s['Type'] = s['Type'].str.split('_',expand=True)[0]

print(s.head(5))
  Account_ID        Type Constituent Id
0     ABCDEF   Household         222222
1     GHIJKL   Household         444444
2     ABCDEF  Individual         111111
3     GHIJKL  Individual         555555
4     ABCDEF  Individual         333333

df3 = pd.merge(df1,
         s,
         on=['Type','Constituent Id'],
         how='left'
        )

print(df3)

  Constituent Id        Type              Name Account_ID
0         111111  Individual       Panda Smith     ABCDEF
1         222222   Household  Panda and Python     ABCDEF
2         333333  Individual      Python Jones     ABCDEF
3         444444   Household   Postgres Family     GHIJKL
4         555555  Individual     Paul Postgres     GHIJKL
5         666666  Individual     Mary Postgres     GHIJKL
6         777777  Individual   Sqlite Postgres     GHIJKL

关于python - 如何处理一个 pandas 数据帧的行中的 ID 组并使用它们从另一个数据帧中提取记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62478228/

相关文章:

javascript - 使用 PKCS7Padding 在 python 和 Node.js 之间进行 AES 加密

python - 如何在 DAG python 代码中使用 Airflow 模板引用

python:改进我读取大型(5GB)txt 文件的方式

python - 标签编码器 : TypeError: '>' not supported between instances of 'float' and 'str'

python - 在 pandas 的数据框中查找并链接值

python - 将一行除以具有相同日期时间的所有其他行的平均值

python - 重命名 pandas 数据框的列名称未按预期工作 - python

python - 在 pandas 中为 Stackoverflow/SO 问题重新生成数据框的代码

r - 在 R 数据帧中生成随机数,这些随机数在相似行中保持不变

python - 用该组中的第一个非空值填充该组中的所有值