我想将一列数据移动到行中并将其拆分为24列。为了更好地理解,我已经将数据集的示例放在下面。这是我的数据集,基本上,我想将“ bb”列划分为24列,其余列将具有与以前相同的信息。因此,从cc列将一列拆分为24列。它将重复直到结束。这是我的数据:
aa, bb, cc, dd, ee, ff, gg, hh, ii, jj, kk, ll, mm, nn
1 1 0.1 0.3 94 60 5 1 -2. 29 25 12 28 0
.
.
1 24 0.1 0.3 94 60 5 1 -2. 29 25 12 28 0
我已经将bb列拆分为该行。我的代码在这里:
import numpy as np
import pandas as pd
dataframe = pd.read_csv('dataset.csv', sep=',')
flg = 0
'''Select the features for moving one column to 24 hours columns '''
for j in range(3, 14):
'''Taking data 0 to 23'''
for i in range(24):
''' select all the features in the hour column where hour value is i '''
index_for_ith_hour_feature = np.where(dataframe.iloc[:, 1] == i)[0]
'''if the flag value 1 it will enter the loop for making one column to 24 hour columns'''
if flg == 1:
'''taking all the data where jth value will be constant and 24 hours features will be selected and stacked horizontally'''
buffer = dataframe.iloc[index_for_ith_hour_feature, j].values.reshape(-1, 1)
'''stack all the data with buffer'''
all_data = np.hstack((all_data, buffer))
else:
'''first loop will comes here because flg=0, after that, it will go to if loop and continue'''
all_data = dataframe.iloc[index_for_ith_hour_feature, j].values.reshape(-1, 1)
flg = 1
'''Select date columns'''
date_data_column = dataframe.iloc[index_for_ith_hour_feature, 0:2].values
'''stacking date columns and all the features previously selected'''
all_data = np.hstack((date_data_column, all_data))
print(all_data)
但是似乎我的代码做错了。我不明白问题出在哪里。到目前为止,它没有显示任何输出。从这里找出错误或实现它的更好方法对我来说真的很有帮助。谢谢。