[英]reorder panda data frame columns vertical
I'm a bit new to panda and have some diabetic data that i would like to reorder. 我对熊猫有点陌生,有一些我想重新排序的糖尿病数据。
I'd like to copy the data from column 'wakeup'
through '23:00:00'
, and put this data vertical under each other so I would get a new dataframe column: 我想从'wakeup'
列复制数据到'23:00:00'
,并将这些数据垂直放置在彼此之间,这样我将获得一个新的dataframe列:
5.6
8.1
9.9
6.3
4.1
13.3
NAN
3.9
3.3
6.8
.....etc
I'm assuming the data is in a dataframe already. 我假设数据已经在数据框中。 You can index the columns you want and then use melt
as suggested. 您可以索引所需的列,然后根据建议使用melt
。 Without any parameters, melt
will 'stack' all your data into one column of a new dataframe. 没有任何参数, melt
会将所有数据“堆叠”到新数据框的一列中。 There's another column created to identify the original column names, but you can drop that if needed. 创建了另一列以标识原始列名,但是如果需要,可以将其删除。
df.loc[:, 'wakeup':'23:00:00'].melt()
variable value
0 wakeup 5.6
1 wakeup 8.1
2 wakeup 9.9
3 wakeup 6.3
4 wakeup 4.1
5 wakeup 13.3
6 wakeup NAN
7 09:30:00 3.9
8 09:30:00 3.3
9 09:30:00 6.8
...
You mention you want this as another column, but there's no way to sensibly add it into your existing dataframe. 您提到要将此作为另一列,但没有办法将其明智地添加到现有数据框中。 The shape likely won't match also. 形状也可能不匹配。
Solved it myself finally took me quite some time. 我自己解决了这个问题,终于花了我很多时间。 Notice here the orginal data was in df1 result in dfAllMeasurements 请注意,原始数据在df1中是dfAllMeasurements中的结果
dfAllMeasurements = df1.loc[:, 'weekday':'23:00:00']
temp = dfAllMeasurements.set_index('weekday','ID').stack(dropna=False) #dropna = keeping NAN
dfAllMeasurements = temp.reset_index(drop=False, level=0).reset_index()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.