第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

按行拆分?jǐn)?shù)據(jù)幀并在python中生成數(shù)據(jù)幀列表

按行拆分?jǐn)?shù)據(jù)幀并在python中生成數(shù)據(jù)幀列表

慕哥6287543 2022-06-14 16:16:48
我有一個(gè)數(shù)據(jù)框:data = {'Timestep'      : [0,1,2,0,1,2,3,0,1],        'Price'           : [5,7,3,5,7,10,8,4,8],        'Time Remaining' : [10.0,10.0,10.0,15.0,15.0,15.0,15.0,12.0,12.0]}df = pd.DataFrame(data, columns = ['Timestep','Price','Time Remaining'])我想將數(shù)據(jù)幀轉(zhuǎn)換為一個(gè)包含多個(gè)數(shù)據(jù)幀的列表,其中每個(gè)時(shí)間步長序列 (0-2,0-3,0-1) 是一個(gè)數(shù)據(jù)幀。此外,我希望時(shí)間步長成為每個(gè)數(shù)據(jù)集中的索引。最后應(yīng)該是這樣的:我有一個(gè)包含數(shù)千行和不規(guī)則序列的數(shù)據(jù)框,所以我想我必須遍歷這些行。有誰知道我該如何解決這個(gè)問題?
查看完整描述

2 回答

?
梵蒂岡之花

TA貢獻(xiàn)1900條經(jīng)驗(yàn) 獲得超5個(gè)贊

據(jù)我了解-每當(dāng)您Timestep達(dá)到 0時(shí),您都需要一個(gè)新的 DataFrame-


這是你可以嘗試的


#This will give you the location of all zeros [0, 3, 7]

zero_indices = list(df.loc[df.Timestep == 0].index)

#We append the number of rows to this to get the last dataframe [0, 3, 7, 9]

zero_indices.append(len(df))

#Then we get the ranges - tuples of consecutive entries in the above list [(0, 3), (3, 7), (7, 9)]

zero_ranges = [(zero_indices[i], zero_indices[i+1]) for i in range(len(zero_indices) - 1)]

#And then we extract the dataframes into a list

list_of_dfs = [df.loc[x[0]:x[1] - 1].copy(deep=True) for x in zero_ranges]


查看完整回答
反對 回復(fù) 2022-06-14
?
慕萊塢森

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊

現(xiàn)在在移動(dòng)設(shè)備上無法對此進(jìn)行測試,但您可以通過以下方式完成:


current_sequence_index = -1

sequences = []

for __, row in data.iterrows():

    if row.Timestep == 0:

        sequences.append(pd.DataFrame())

        current_sequence_index += 1


    sequences[current_sequence_index].append(row, ignore_index=True)   

本質(zhì)上,這將遍歷您的數(shù)據(jù)并在 Timestep 為 0 時(shí)生成一個(gè)新的 DataFrame。此解決方案有一些假設(shè):1. Timestep 的開始始終為 0。 2. Timesteps 始終是順序的。


查看完整回答
反對 回復(fù) 2022-06-14
  • 2 回答
  • 0 關(guān)注
  • 164 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號