Python Dataframes在计数器字段上循环



我想建立一个循环,函数将在"计数器"字段创建子集数据帧,然后将它们连接回一起

下面是我想要的一个小例子,实际上计数器可以是数百个:

data = {'name': ['Adam','Adam','Adam','Adam', 'nick','nick','nick','nick','nick','krish','krish','krish',],
'age': [20,20,20,20, 21,21,21,21,21, 19,19,19],
'Product': ['A','B','C','D','A','B','C','D','E','A','B','C'],
'Counter': [1,2,3,4,1,2,3,4,5,1,2,3]}

# Create DataFrame
df = pd.DataFrame(data)

df1 = df.loc[df['Counter'] == 1]
df1 = df1[['name','age','Product']]
df2 = df.loc[df['Counter'] == 2]
df2 = df2[['name','age','Product']]
df3 = df.loc[df['Counter'] == 3]
df3 = df3[['name','age','Product']]
df4 = df.loc[df['Counter'] == 4]
df4 = df4[['name','age','Product']]
df5 = df.loc[df['Counter'] == 5]
df5 = df5[['name','age','Product']]
dfs = [df1,df2,df3,df4,df5]
from functools import reduce
df_merged = reduce(lambda  left,right: pd.merge(left,right,on=['name'], how='outer'), dfs)

我相信你在寻找些什么叫做df.pivot:

x = df.pivot(index="name", columns="Counter")
x = x[sorted(x.columns, key=lambda x: (x[1], x[0]))]
x.columns = [f"{a}_{b}" for a, b in x.columns.values]
print(x)

打印:

Product_1  age_1 Product_2  age_2 Product_3  age_3 Product_4  age_4 Product_5  age_5
name                                                                                      
Adam          A   20.0         B   20.0         C   20.0         D   20.0       NaN    NaN
krish         A   19.0         B   19.0         C   19.0       NaN    NaN       NaN    NaN
nick          A   21.0         B   21.0         C   21.0         D   21.0         E   21.0

相关内容

  • 没有找到相关文章

最新更新