pandas generates wide table from single entry data set

demand

Scenes

A large amount of physical examination data is derived from the hospital database, but in the physical examination data table, each row represents the result of a certain physical examination of a certain person. The purpose is to store each physical examination result of each person as a row, and each column is a physical examination item.

Example

Raw data

StuID Type on one
0 111021 Math 89
1 111021 English 93
2 312983 English 91
3 314621 English 82
4 314621 Math 92
5 112341 Math 82

Purpose: Convert into the following table

StuID English Math
0 111021 93 89
1 312983 91 NaN
2 314621 82 92
3 112341 NaN 82

Option One

Created with Raphaël 2.2.0 开始 提取唯一ID(可以是一行或者多行唯一确定) 确定并创建结果的DataFram结构,导入唯一ID 通过唯一ID,更新结果DF的数据 结束
  • The specific code is as follows
#将’B'列的类别调整为行。
#1
num = df[~df.duplicated(subset=['StuID'])].loc[:,'StuID'].to_list()
#2
result_df = pd.DataFrame({
    
    'StuID': np.array(num)},columns=['StuID','English','Math'])
#3
for i in df.index:
    t = df.loc[i,'Type']
    num = df.loc[i,'StuID']
    result_df.loc[result_df['StuID'] == num,[t]] = df.loc[i,'Num']
print(result_df)
  • result

Insert picture description here

Guess you like

Origin blog.csdn.net/u013894391/article/details/104525345