假設您的數據源是一個CSV文件,
from pandas.io.parsers import read_csv
df = read_csv("radar_data.csv")
df # shows what is in df
loc speed time
0 A 63 0
1 B 61 0
2 C 63 0
3 D 65 0
4 A 73 5
5 B 73 5
6 C 75 5
7 D 75 5
8 A 67 0
9 B 68 0
10 C 68 0
11 D 70 0
請注意,我沒有設置loc
作爲指標又那麼它使用一個自動增量整數索引。
panel = df.set_index(['loc', 'time']).sortlevel(0).to_panel()
但是,如果你的數據幀已經使用loc
爲指標,我們需要將time
列追加到其中,因此,我們有一個LOC-時間分級指數。這可以使用方法中的新append
選項完成。像這樣的: -
panel = df.set_index(['time'], append=True).sortlevel(0).to_panel()
在任何情況下,我們應該在這種情況下到達: -
panel # shows what panel is
<class 'pandas.core.panel.Panel'>
Dimensions: 1 (items) x 4 (major) x 2 (minor)
Items: speed to speed
Major axis: A to D
Minor axis: 0 to 5
panel["speed"] # <--- This is what you are looking for.
time 0 5
loc
A 63 67
B 73 61
C 68 73
D 63 68
希望這有助於。
啊......好戲。謝謝! –
我得到「ReshapeError:索引包含重複條目,無法重新塑造」的結果。我的一些位置是帶有空格的字符串,可能是相關的嗎? –
葉。對我也不起作用。我得到'ReshapeError:索引包含重複的條目,也無法重塑。 –