python - 使用具有相同列的数据创建新列

标签 python pandas dataframe

我有与此类似的 DataFrame。如何添加具有在列之一中具有相同值的行名称的新列?例如:
有这个:

  name  building 
  a     blue
  b     white
  c     blue
  d     red
  e     blue
  f     red
如何得到这个?
  name  building  in_building_with
  a     blue      [c, e]
  b     white     []
  c     blue      [a, e]
  d     red       [f]
  e     blue      [a, c]
  f     red       [d]

最佳答案

这是我只能想到的方法(最糟糕的):

r = df.groupby('building')['name'].agg(dict)
df['in_building_with'] = df.apply(lambda  x: [r[x['building']][i] for i in (r[x['building']].keys()-[x.name])], axis=1)

df:
name    building    in_building_with
0   a   blue    [c, e]
1   b   white   []
2   c   blue    [a, e]
3   d   red     [f]
4   e   blue    [a, c]
5   f   red     [d]

联系方式:
  • 制作一本字典,它会给出建筑物发生的索引。
  • building
    blue     {0: 'a', 2: 'c', 4: 'e'}
    red              {3: 'd', 5: 'f'}
    white                    {1: 'b'}
    dtype: object
    
  • 从列表中减去当前建筑物的索引,因为您正在查看它以外的元素以获得外观索引。
  • r[x['building']].keys()-[x.name]
    
  • 获取这些索引处的值并将它们放入一个列表中。
  • 关于python - 使用具有相同列的数据创建新列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65182169/

    相关文章:

    python - 根据匹配从另一个数据帧计算数据帧字段值

    python - 如何为 Python 的 MD5 模块设置加密 key ?

    Python: 'from module' 与 'from__main__' 使用 timeit 的奇怪行为

    python - 从符号多项式获取向量空间坐标

    python - 在python中将所有列表值连接到一个数组

    python - 如何在pandas中groupby之后获得两组之间的p值?

    python - 返回所有相等的列

    python - 将数据帧向量添加到数据帧表

    python-3.x - Python3 如何从 while 循环设置 DataFrame

    r - 获取最频繁的条目