我有一个DataFrame'df',并且有53个不同的列和1740行。列包括; 'Age','RaceOne','RaceTwo','RaceThree','Name','Identity'等。但是我想重新组织DataFrame,以便新的变量'RaceTimes'代替'RaceOne','RaceTwo' ,“ RaceThree”和其余DataFrame列以特定方式遵循,如下面的第二个DataFrame所示...
当前df:
'Age' 'RaceOne' 'RaceTwo' 'RaceThree' 'Name' 'Identity' ... 'Male/Female
25 15:40:00 15:35:00 15:39:00 Wendy 105888 ... Female
26 15:43:00 15:25:00 15:15:00 Steve 114342 ... Male
22 15:20:00 15:31:00 15:23:00 Ant 123553 ... Male
我想看的东西...
'Age' 'RaceTimes' 'Name' 'Identity' ... 'Male/Female'
25 15:40:00 Wendy 105888 ... Female
25 15:35:00 Wendy 105888 ... Female
25 15:39:00 Wendy 105888 ... Female
26 15:43:00 Steve 114342 ... Male
26 15:25:00 Steve 114342 ... Male
26 15:15:00 Steve 114342 ... Male
22 15:20:00 Ant 123553 ... Male
22 15:31:00 Ant 123553 ... Male
22 15:23:00 Ant 123553 ... Male
答案 0 :(得分:1)
IIUC,检查pd.wide_to_long
:
final=(pd.wide_to_long(df,'Race',i='Age',j='v',sep='',suffix='\w+').reset_index(1,drop=True)
.sort_index().reset_index())
Age Male/Female Name Identity ... Race
0 22 Male Ant 123553 ... 15:20:00
1 22 Male Ant 123553 ... 15:31:00
2 22 Male Ant 123553 ... 15:23:00
3 25 Female Wendy 105888 ... 15:40:00
4 25 Female Wendy 105888 ... 15:35:00
5 25 Female Wendy 105888 ... 15:39:00
6 26 Male Steve 114342 ... 15:43:00
7 26 Male Steve 114342 ... 15:25:00
8 26 Male Steve 114342 ... 15:15:00
答案 1 :(得分:0)
首先,请仔细研究。
Pandas已内置解决您的问题的方法。 您可以使用 pd.melt 将DataFrame从宽格式转换为长格式。 看看示例。很简单。