[英]Given a list of indexes, how do I split a DataFrame on those indexes?
[英]Pandas DataFrame group-by indexes matching list - indexes respectively smaller than list[i+1] and greater than list[i]
我有一個 DataFrame Times_df,時間在單個列中,第二個 DataFrame End_df 具有按組名索引的每個組的特定結束時間。
Times_df = pd.DataFrame({'time':np.unique(np.cumsum(np.random.randint(5, size=(100,))), axis=0)})
End_df = pd.DataFrame({'end time':np.unique(random.sample(range(Times_df.index.values[0], Times_df.index.values[-1]), 10))})
End_df.index.name = 'group'
我想為 Times_df 中的所有時間添加一個組索引,該索引小於或等於 End_df 中的每個連續結束時間但大於前一個
我現在只能用一個循環來做這件事,這需要永遠;(
lis = []
i = 1
for row in Times_df['time'].values:
while i <= row:
lis.append((End_df['end time']==row).index)
i +1
然后我將列表 lis 作為新列添加到 Times_df
Times_df['group']=lis
可悲的是仍然使用循環的另一個解決方案是:
test_df = pd.DataFrame()
for group, index in End_df.iterrows():
test = count.loc[count.index<=index['end time]][:]
test['group']=group
test_df = pd.concat([test_df,test], axis=0, ignore_index=True)
我認為您正在尋找的是pd.cut將您的價值觀分類到組中。
bins = [0, 3, 10, 20, 53, 59, 63, 65, 68, 74, np.inf]
groups = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Times_df["group"] = pd.cut(Times_df["time"], bins, labels=groups)
print(Times_df)
time group
0 2 0
1 3 0
2 7 1
3 11 2
4 15 2
5 16 2
6 18 2
7 22 3
8 25 3
9 28 3
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.