[英]stuck on how to fill multiIndexed column dataframe
我創建了一個 dataframe ,其中包含來自列表的多索引列,使用:
trans = ['Scale Up', 'Scale Down', 'U']
sources = ['P1', 'P2']
cols = ['date', 'sec']
index = pd.MultiIndex.from_product([trans, sources, cols])
df=pd.DataFrame(columns=index)
df 是我應該填寫的 dataframe ,只是為了打印和理解我使用的列:
index.to_frame().transpose()
對於每個 trans 和 source 我想根據另一個數據框 new_positions 填充日期和秒:
my_data=new_positions.loc[((new_positions['trans']=='Scale Up') & (new_positions['source'] == 'P1'))].sort_values(['score']).tail(10)[['date', 'sec']].copy()
整個 dataframe (秒和日期)應該用 level0 和 1 上的循環填充。這是我想為 level0='Scale Up' 和 level1='P1' 填充秒和日期的方法
通過DataFrame.set_index
使用DataFrame.stack
和 Series.unstack MultiIndex in columns
創建Series.unstack
:
new_positions = pd.DataFrame({'trans':['Scale Up'] * 4 + ['Scale Down'] * 4,
'source':['P1'] * 2 + ['P2'] * 2 + ['P1'] * 2 + ['P2'] * 2,
'date':pd.date_range('2021-01-01', periods=8),
'sec':[1,2,3,4,5,6,7,8]})
print (new_positions)
trans source date sec
0 Scale Up P1 2021-01-01 1
1 Scale Up P1 2021-01-02 2
2 Scale Up P2 2021-01-03 3
3 Scale Up P2 2021-01-04 4
4 Scale Down P1 2021-01-05 5
5 Scale Down P1 2021-01-06 6
6 Scale Down P2 2021-01-07 7
7 Scale Down P2 2021-01-08 8
s = new_positions.groupby(['trans','source']).cumcount()
df = (new_positions.assign(idx=s)
.set_index(['trans','source','idx'])[['date', 'sec']]
.stack()
.unstack([0,1,3]))
print (df)
trans Scale Up Scale Down
source P1 P2 P1 P2
date sec date sec date sec date sec
idx
0 2021-01-01 1 2021-01-03 3 2021-01-05 5 2021-01-07 7
1 2021-01-02 2 2021-01-04 4 2021-01-06 6 2021-01-08 8
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.