我有一個數據幀稱爲raw_df
:如何連接pandas.DataFrames列
columns = ['force0', 'distance0', 'force1', 'distance1']
raw_data = [{'force0': 1.2, 'distance0': 0.0, 'force1': 0.5, 'distance1': 0.0},
{'force0': 1.3, 'distance0': 0.1, 'force1': 0.6, 'distance1': 0.0},
{'force0': 1.4, 'distance0': 0.2, 'force1': 0.7, 'distance1': 0.3},
{'force0': 1.5, 'distance0': 0.5, 'force1': 0.8, 'distance1': 0.6}]
raw_df = pd.DataFrame(raw_data, columns=columns)
raw_df
看起來是這樣的:
force0 distance0 force1 distance1
0 1.2 0.0 0.5 0.0
1 1.3 0.1 0.6 0.0
2 1.4 0.2 0.7 0.3
3 1.5 0.5 0.8 0.6
目前沒有指標,但我想的距離列合併爲一個索引,所以列是:
force0 force1
distance
0.0 1.2 0.5
0.0 NaN. 0.6
0.1 1.3 NaN
0.2 1.4 NaN
0.3 NaN 0.7
0.5 1.5 NaN
0.6 NaN 0.8
請注意,distance1 = 0.0時force1中有2個條目。
索引(距離)不應該排序:它們增加然後可變地減少,並且每個測試的原始順序是重要的。
Stefan對我描述不清的問題發表了一個驚人的答案,但它似乎用其他數字填補了任何失蹤的力量(這會誤導人,因爲這些測試中沒有對這些距離進行力測量)。我使用np.nan
作爲缺失值,因爲我認爲這是pandas
所做的。
我認爲merge
或join
可能會做我所需要的但不能理解的the docs。
也許pandas.DataFrame
不是爲這樣的數據,我應該使用numpy.genfromtxt
代替,只是選擇我需要在飛行中的列:我看不出有任何優勢,使用上飛一個pandas.DataFrame
,如果我選擇列(因爲在這種情況下我沒有使用索引)。
感謝您的任何幫助。
如果要處理原始數據幀的示例,會不錯。 –
你的'test_ids'是什麼? 「距離1」,「距離2」等所有值都是唯一的嗎? –
test_ids只是與測試編號對應的整數列表。例如,第一個是1,如在力1中 – blokeley