[英]Setting values in a pandas MultiIndex series
我試圖以多指數值將一系列設置為另一個系列。 沒有復雜的黑客,我找不到在熊貓中做到這一點的方法。
我原來的系列:
one 1 0.522764
3 0.362663
7 0.963108
two 2 0.717855
4 0.004645
5 0.077471
我要連接的數據,在three
級:
2 0.8
7 0.9
8 0.7
所需的輸出:
one 1 0.522764
3 0.362663
7 0.963108
two 2 0.717855
4 0.004645
5 0.077471
three 2 0.800000
7 0.900000
8 0.700000
我無法想出在熊貓中做到這一點的優雅方式。 我所能做的就是以下黑客攻擊:
# imports
import numpy as np
import pandas as pd
# to replicate the Series:
np.arrays = [['one','one','one','two','two','two'],[1,3,7,2,4,5]]
my_series = pd.Series([np.random.random() for i in range(6)],
index=pd.MultiIndex.from_tuples(list(zip(*np.arrays))))
# the new data I need to add:
new_data = pd.Series({1: .9, 2: .7, 3: .8})
以下是我目前正在解決的問題:
# rename the index so that I can call it later
new_data.index.name = 'level_1'
# turn it into temporary a dataframe so that I can add a new column
temp = pd.DataFrame(new_data)
# create a new column with the desired name for first index level
temp['level_0'] = 'three'
# reset index, set the new index, turn into Series again
temp = temp.reset_index().set_index(['level_0', 'level_1'])[0]
# append it to the larger dataframe
my_series = my_series.append(temp)
這產生了所需的輸出。
問題:在熊貓中有一種簡單,優雅的方式嗎?
您可以嘗試使用pd.concat
:
u = (new_data.to_frame()
.assign(_='three')
.set_index(['_', new_data.index])[0])
pd.concat([df, u])
one 1 0.618472
3 0.026207
7 0.766849
two 2 0.651633
4 0.282038
5 0.160714
three 1 0.900000
2 0.700000
3 0.800000
dtype: float64
選項1
pd.concat
是一種使用keys
參數預先添加索引或列級別的方法。 將其與第二個pd.concat
結合起來完成工作。
pd.concat([my_series, pd.concat([new_data], keys=['Three'])])
one 1 0.943246
3 0.412200
7 0.379641
two 2 0.883960
4 0.182983
5 0.773227
Three 1 0.900000
2 0.700000
3 0.800000
dtype: float64
選項2
或者我們可以在index
參數中插入一個額外的數組時構造一個新系列。 再次使用pd.concat
進行組合。 注意我可以使用pd.MultiIndex.from_arrays
但只是通過將數組直接傳遞給index
參數來簡化語法。
pd.concat([
my_series,
pd.Series(new_data.values, [['Three'] * new_data.size, new_data.index])
])
one 1 0.943246
3 0.412200
7 0.379641
two 2 0.883960
4 0.182983
5 0.773227
Three 1 0.900000
2 0.700000
3 0.800000
dtype: float64
選項3
另一種用多索引重建系列的方法。 這個使用pd.MultiIndex.from_product
。
pd.concat([
my_series,
pd.Series(new_data.values, pd.MultiIndex.from_product([['Three'], new_data.index]))
])
one 1 0.943246
3 0.412200
7 0.379641
two 2 0.883960
4 0.182983
5 0.773227
Three 1 0.900000
2 0.700000
3 0.800000
dtype: float64
如果你開始使用new_data
的等效多索引,你可以直接將Series
es與pd.concat
連接pd.concat
而不會強制轉換到DataFrame
並返回,如:
new_series = pd.Series([0.8,0.9,0.7],
index=pd.MultiIndex.from_tuples([('three',x) for x in range(1,4)])
)
pd.concat([my_series,new_series]) #note OP changed name of orig series from df to my_series
#==============================================================================
# one 1 0.236158
# 3 0.699102
# 7 0.421937
# two 2 0.887081
# 4 0.520304
# 5 0.211461
# three 1 0.800000
# 2 0.900000
# 3 0.700000
# dtype: float64
#==============================================================================
type(pd.concat([my_series,new_series])) # pandas.core.series.Series
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.