簡體   English   中英

如何從另一列的所有值創建新的列名並按 pandas dataframe 中的另一列創建新列名?

[英]how to create new column names from another column all values and agg by another column in pandas dataframe?

我試過 pivot 表,但沒有用。 我想知道怎么做?

輸入數據框:

df_test = pd.DataFrame({'users': ['user1','user1','user2','user2'],
 'day' : [2,2,2,2],
 'col':['a','b','c','d']})

輸入df

我想得到這樣的東西:

輸出df

對於這個特定的例子,您可以使用以下內容:

df_test = pd.DataFrame({'users': ['user1','user1','user2','user2'],
 'day' : [2,2,2,2],
 'col':['a','b','c','d']})

df_out = df_test.set_index(['users', 'day', df_test.groupby(['users', 'day']).cumcount()])[['col']].unstack([1,2])

df_out.columns = [f'{i}_day{j}_another{j}' if k != 0 else f'{i}_day{j}' for i, j, k in df_out.columns]
print(df_out.reset_index())

Output:

   users col_day2 col_day2_another2
0  user1        a                 b
1  user2        c                 d

您可以在旋轉之前構建label列 ( col_dayX_anotherY ):

df_test['day_count'] = df_test.groupby(['users', 'day']).cumcount().add(1)

df_test['label'] = df_test.agg(lambda x: f'col_day{x.day}_another{x.day_count}', axis=1)
df_test['label'] = df_test.label.str.strip('_another1')

# users  day col  day_count              label
# user1    2   a          1           col_day2
# user1    2   b          2  col_day2_another2
# user2    2   c          1           col_day2
# user2    2   d          2  col_day2_another2

然后你可以pivot()

df_test = df_test.pivot(index='users', columns='label', values='col')

#       col_day2 col_day2_another2
# users                           
# user1        a                 b
# user2        c                 d

這是一種更通用的方法,在需要時為新的日子生成列。 請注意,某些數據元素可能會丟失(由 NaN 自動填充)。 (多個)重復列由“_another”擴展。

df_test = pd.DataFrame({'users': ['user1','user1','user1','user1',
                                  'user2','user2','user2','user3'],
    'day':[1,2,2,2,1,2,2,3],
    'col':['a','b','c','d','e','f','g','h']})
# Create empty dictionary
dd={}
# Loop across rows in dataframe
for i in range(len(df_test)):
    user=df_test.users[i]
    # add dictionary entry for each user (set dict with first col on new user)
    if not user in dd:dd[user]={'users':user}
    # set corresponding new field for col+day+daynr
    newField='col_day'+str(df_test.day[i])
    # if field exists already, extend the name
    while newField in dd[user]: newField+='_another'
    # now set the value
    dd[user][newField]=df_test.col[i]
# create new data frame
df_new=pd.DataFrame(dd.values())

df_new 產生了這個例子,如下所示:

   users col_day1 col_day2 col_day2_another col_day2_another_another col_day3
0  user1        a        b                c                        d      NaN
1  user2        e        f                g                      NaN      NaN
2  user3      NaN      NaN              NaN                      NaN        h

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM