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
- 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