【问题标题】:How to collapse pandas rows for select column values to minimal combinations and map back to original rows如何将选择列值的熊猫行折叠为最小组合并映射回原始行
【发布时间】:2019-06-04 03:27:02
【问题描述】:

上下文:

我有一个包含 7 列(味道、颜色、温度、纹理、形状、age_of_participant、name_of_participant)的 pandas 数据框。

在 7 列中,味道、颜色、温度、质地和形状可以在多行中有重叠的值(即味道可能不止一行是酸的)

我正在尝试将所有行折叠成给定的最少数量的组合 味道、颜色、温度、纹理和形状值,而忽略 NA(换句话说,覆盖它们)。下一部分是将这些行中的每一行映射到原始行。

模拟数据集:


data_set = [
   {'color':'brown', 'age_of_participant':23, 'name_of_participant':'feb'},
   {'taste': 'sour', 'color':'green', 'temperature': 'hot', 'age_of_participant':16,'name_of_participant': 'joe'},
   {'taste': 'sour', 'color':'green', 'texture':'soft', 'age_of_participant':17,'name_of_participant': 'jane'},
   {'color':'green','age_of_participant':18,'name_of_participant': 'jeff'},
   {'taste': 'sweet', 'color':'red', 'age_of_participant':19,'name_of_participant': 'joke'},
   {'taste': 'sweet', 'temperature': 'cold',  'age_of_participant':20,'name_of_participant': 'jolly'},
   {'taste': 'salty', 'color':'purple', 'texture':'soft', 'age_of_participant':21,'name_of_participant': 'jupyter'},
   {'taste': 'salty', 'color':'brown', 'age_of_participant':22,'name_of_participant': 'january'}

]

import pandas as pd
import random

data_set = random.sample(data_set, k=len(data_set))

data_frame = pd.DataFrame(data_set)

print(data_frame)

   age_of_participant   color name_of_participant  taste temperature texture
0                  16   green                 joe   sour         hot     NaN
1                  17   green                jane   sour         NaN    soft
2                  18   green                jeff    NaN         NaN     NaN
3                  19     red                joke  sweet         NaN     NaN
4                  20     NaN               jolly  sweet        cold     NaN
5                  21  purple             jupyter  salty         NaN    soft
6                  22   brown             january  salty         NaN     NaN

我的尝试:


# These columns are used to do the grouping since age_of_participant and name_of_participant are unique per row

values_that_can_be_grouped = ['taste', 'color','temperature','texture']

sub_set = data_frame[values_that_can_be_grouped].drop_duplicates().reset_index(drop=False)

my_unique_set = sub_set.groupby('taste', as_index=False).first()

print(my_unique_set)


   taste  index  color temperature texture
0             2  green                    
1  salty      6  brown                    
2   sour      1  green                soft
3  sweet      4               cold        



此时我不太确定如何将上面的行映射到除索引 2、6、1、4 之外的所有原始行。我检查了pandas code,看起来其他索引没有保存在任何地方?

我想要达到的目标:


   age_of_participant   color name_of_participant  taste temperature texture
0                  16   green                 joe   sour         hot    soft
1                  17   green                jane   sour         hot    soft
2                  18   green                jeff   sour         hot    soft
3                  19     red                joke  sweet        cold     NaN
4                  20     red               jolly  sweet        cold     NaN
5                  21  purple             jupyter  salty         NaN    soft
6                  22   brown             january  salty         NaN     NaN


【问题讨论】:

  • data_frame.assign(color=data_frame.color.ffill()).groupby('color').apply(lambda x: x.ffill().bfill())

标签: python python-3.x pandas dataframe


【解决方案1】:
data_frame.assign(color=data_frame.color.ffill()).groupby('color').apply(lambda x: x.ffill().bfill())
Out[1089]: 
   age_of_participant   color name_of_participant  taste temperature texture
0                  16   green                 joe   sour         hot    soft
1                  17   green                jane   sour         hot    soft
2                  18   green                jeff   sour         hot    soft
3                  19     red                joke  sweet        cold     NaN
4                  20     red               jolly  sweet        cold     NaN
5                  21  purple             jupyter  salty         NaN    soft
6                  22   brown             january  salty         NaN     NaN

【讨论】:

  • assign 中的填充可能会导致问题
  • @WeNYoBen 有哪些问题?告诉我
  • 你假设 df 有序
  • @WeNYoBen 我明白你的意思:好吧,我想任何修改都会使它和你的一样
  • 如果颜色有一个空条目怎么办?并且是未排序的?在这种情况下不起作用 - 将更新模拟数据以使其清晰
【解决方案2】:

IIUC 我觉得每个tastecolor 都使用ffillbfill,然后groupby 他们在这里更安全

df.taste.fillna(df.groupby('color').taste.apply(lambda x : x.ffill().bfill()),inplace=True)
df.color.fillna(df.groupby('taste').color.apply(lambda x : x.ffill().bfill()),inplace=True)
df=df.groupby(['color','taste']).apply(lambda x : x.ffill().bfill())
df
   age_of_participant   color   ...   temperature texture
0                  16   green   ...           hot    soft
1                  17   green   ...           hot    soft
2                  18   green   ...           hot    soft
3                  19     red   ...          cold     NaN
4                  20     red   ...          cold     NaN
5                  21  purple   ...           NaN    soft
6                  22   brown   ...           NaN     NaN
[7 rows x 6 columns]

【讨论】:

  • 兼顾色泽+1
  • 数据未排序是否有效?此外,颜色可能为空
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-05-19
  • 2020-03-19
  • 2020-04-20
  • 1970-01-01
  • 2015-12-01
  • 1970-01-01
  • 2020-08-06
相关资源
最近更新 更多