python - Pandas DataFrame 重塑(很多行变成很长的列)
问题描述
我在 Pandas 中有以下形状的数据,可与 scikit-learn 一起使用。
temp2004[["station_id"] + hours]
Out[112]:
station_id h1 h2 h3 h4 h5 h6 ... h18 h19 h20 h21 h22 h23 h24
0 1 43 44 42 34 30 35 ... 53 43 37 36 36 43 44
1 1 45 46 47 46 46 45 ... 52 46 45 42 43 44 47
2 1 46 46 42 41 42 41 ... 69 65 64 60 61 62 61
3 1 62 62 60 60 60 60 ... 70 67 67 63 63 63 64
4 1 64 61 62 61 62 62 ... 62 60 60 58 57 53 51
... .. .. .. .. .. .. ... ... ... ... ... ... ... ...
16561 11 29 30 30 30 29 29 ... 30 29 28 27 27 25 22
16562 11 21 20 20 19 19 18 ... 36 33 33 35 35 34 35
16563 11 35 36 36 38 37 37 ... 55 50 50 49 47 47 48
16564 11 46 43 40 37 36 36 ... 51 50 50 47 45 46 44
16565 11 44 45 45 41 40 38 ... 59 54 51 52 51 48 52
[4026 rows x 25 columns]
我需要稍微重塑这个数组,使 station_id 成为特征,hx 值成为它们下方的一列样本。
我尝试仅使用单个 station_id 尝试使用 Pandas stack() 方法,结果如下:
temp.stack()
Out[72]:
11340 h1 36
h2 32
h3 31
h4 30
h5 34
..
11705 h20 55
h21 55
h22 56
h23 54
h24 53
Length: 8784, dtype: int64
这正是我正在寻找的,但我需要所有其他站 ID 的列。有没有好的方法来做到这一点?最坏的情况,我认为我可以为每个站点创建一个列并将它们组合起来。
为了让这一列与 scikit-learn 一起工作,我必须执行以下操作:
load = load2004[load2004.zone_id == zoneNumber][hours];
temp = temp2004[temp2004.station_id == zonetobeststation[zoneNumber]][hours];
temp_x = temp.stack().values.reshape(-1, 1);
load_y = load.stack().values.reshape(-1, 1);
temp_train, temp_test, load_train, load_test = train_test_split(temp_x, load_y, test_size=.2, random_state=89986);
dtr = tree.DecisionTreeRegressor(random_state=0);
dtr.fit(temp_train, load_train);
gbr = ensemble.GradientBoostingRegressor(max_depth=10);
gbr.fit(temp_train, load_train.ravel());
dtr.score(temp_test, load_test)
gbr.score(temp_test, load_test.ravel())
两个堆叠的结果都需要重新整形,我需要.ravel()
在目标向量上使用。我担心的是,如果我只是盲目地从每个站中制作列,我将无法正确地为 scikit-learn 塑造它们。