简体   繁体   English

垂直重新排列熊猫数据框列

[英]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.

相关问题 将熊猫数据帧的> = 2列总和为单个/标量值的最佳方法 - Best way to sum >=2 columns of a panda data frame into a single/scalar value 有一个熊猫数据框,条件后的 output 特定列 - Having a panda data frame, output specific columns after condition 熊猫数据框:如何将1和0的列合并到新列 - Panda Data frame : How to merge columns of 1 and 0 to a new column 如何在熊猫数据框中交换两列并翻转第三列? - how to swap two columns and flip a third in panda data frame? 如何将列表元素转置/分离到熊猫数据框列中 - How to transpose/separate list elements into panda data frame columns 保留熊猫数据框的第一行和最后一行重复列值 - Keep first and last rows of repetitive columns values of a panda data frame 一种遍历行和列的方法(在熊猫数据框中),select 根据条件将行和列放入熊猫另一个数据框 - A way to iterate through rows and columns (in a panda data frame), select rows and columns based on conditions to put into panda another data frame 有没有办法为数百个属性重新排序 pandas 数据框中的列? - Is there a way to reorder columns in a pandas data frame for hundreds of attributes? pandas从分组数据框中重新排序列的子集 - pandas reorder subset of columns from a grouped data frame 如何对熊猫数据框进行升采样 - How to upsample a panda data frame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM