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

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

對于數(shù)據(jù)幀的每一列,大熊貓會掉落在第一個有效索引之前和最后一個有效索引之后

對于數(shù)據(jù)幀的每一列,大熊貓會掉落在第一個有效索引之前和最后一個有效索引之后

喵喵時光機 2021-03-31 04:11:04
我有一個這樣的數(shù)據(jù)框:df = pd.DataFrame({'timestamp':pd.date_range('2018-01-01', '2018-01-02', freq='2h', closed='right'),'col1':[np.nan, np.nan, np.nan, 1,2,3,4,5,6,7,8,np.nan], 'col2':[np.nan, np.nan, 0, 1,2,3,4,5,np.nan,np.nan,np.nan,np.nan], 'col3':[np.nan, -1, 0, 1,2,3,4,5,6,7,8,9], 'col4':[-2, -1, 0, 1,2,3,4,np.nan,np.nan,np.nan,np.nan,np.nan]              })[['timestamp', 'col1', 'col2', 'col3', 'col4']]看起來像這樣:             timestamp  col1  col2  col3  col40  2018-01-01 02:00:00   NaN   NaN   NaN  -2.01  2018-01-01 04:00:00   NaN   NaN  -1.0  -1.02  2018-01-01 06:00:00   NaN   0.0   NaN   0.03  2018-01-01 08:00:00   1.0   1.0   1.0   1.04  2018-01-01 10:00:00   2.0   NaN   2.0   2.05  2018-01-01 12:00:00   3.0   3.0   NaN   3.06  2018-01-01 14:00:00   NaN   4.0   4.0   4.07  2018-01-01 16:00:00   5.0   NaN   5.0   NaN8  2018-01-01 18:00:00   6.0   NaN   6.0   NaN9  2018-01-01 20:00:00   7.0   NaN   7.0   NaN10 2018-01-01 22:00:00   8.0   NaN   8.0   NaN11 2018-01-02 00:00:00   NaN   NaN   9.0   NaN現(xiàn)在,我想找到一種有效且有效的方法來刪除第一個有效索引之前和之后的有效索引(對于每一列!不計算時間戳)。在此示例中,我有4列,但實際上,我有更多列,大約600列。我正在尋找一種方法來斬波第一個有效索引之前的所有NaN值,以及最后一個有效索引之后的所有NaN值。我猜一種方法是循環(huán)遍歷。但是還有更好的方法嗎?這種方式必須有效。我試圖使用melt“取消透視圖”數(shù)據(jù)框,但這無濟于事。明顯的一點是,斬波后每一列的行數(shù)會有所不同。因此,我希望結(jié)果是一個帶有時間戳和相關(guān)列的數(shù)據(jù)幀列表(每列一個)。例如:             timestamp  col1   3  2018-01-01 08:00:00   1.0  4  2018-01-01 10:00:00   2.0   5  2018-01-01 12:00:00   3.0   6  2018-01-01 14:00:00   NaN   7  2018-01-01 16:00:00   5.0   8  2018-01-01 18:00:00   6.0   9  2018-01-01 20:00:00   7.0   10 2018-01-01 22:00:00   8.0    我的嘗試我這樣嘗試過:final = []columns = [c for c in df if c !='timestamp']for col in columns:    first = df.loc[:, col].first_valid_index()    last = df.loc[:, col].last_valid_index()    final.append(df.loc[:, ['timestamp', col]].iloc[first:last+1, :])
查看完整描述

3 回答

?
慕田峪7331174

TA貢獻1828條經(jīng)驗 獲得超13個贊

您可以使用函數(shù)式編程的功能,并將函數(shù)應(yīng)用于每一列。這可能會加快速度。同樣,當您timestamps看起來已排序時,可以將它們用作Datarame的索引。


df.set_index('timestamp', inplace=True)


final = []

def func(col):

    first = col.first_valid_index()

    last = col.last_valid_index()

    final.append(col.loc[first:last])

    return


df.apply(func)

另外,您可以將所有東西壓緊在一個襯里中:


final = []

df.apply(lambda col: final.append(col.loc[col.first_valid_index() : col.last_valid_index()]))



查看完整回答
反對 回復(fù) 2021-04-13
?
慕村225694

TA貢獻1880條經(jīng)驗 獲得超4個贊

一種想法是在將索引設(shè)置為之后使用列表或字典理解timestamp。您應(yīng)該對數(shù)據(jù)進行測試,以查看這是否可以解決性能問題。如果您的限制是內(nèi)存,則不太可能有幫助。


df = df.set_index('timestamp')


final = {col: df[col].loc[df[col].first_valid_index(): df[col].last_valid_index()] \

         for col in df}


print(final)


{'col1': timestamp

2018-01-01 08:00:00    1.0

2018-01-01 10:00:00    2.0

2018-01-01 12:00:00    3.0

2018-01-01 14:00:00    4.0

2018-01-01 16:00:00    5.0

2018-01-01 18:00:00    6.0

2018-01-01 20:00:00    7.0

2018-01-01 22:00:00    8.0

Name: col1, dtype: float64,

...

'col4': timestamp

2018-01-01 02:00:00   -2.0

2018-01-01 04:00:00   -1.0

2018-01-01 06:00:00    0.0

2018-01-01 08:00:00    1.0

2018-01-01 10:00:00    2.0

2018-01-01 12:00:00    3.0

2018-01-01 14:00:00    4.0

Name: col4, dtype: float64}


查看完整回答
反對 回復(fù) 2021-04-13
?
精慕HU

TA貢獻1845條經(jīng)驗 獲得超8個贊

我的方法是NaN為每列及其倒數(shù)找到的累加總和,并過濾那些大于的條目0。然后,我進行dict理解以為每一列返回一個數(shù)據(jù)框(如果您愿意,可以將其更改為列表)。


例如,我們有


cols = [c for c in df.columns if c!='timestamp']


result_dict = {c: df[(df[c].notnull().cumsum() > 0) &

                     (df.ix[::-1,c].notnull().cumsum()[::-1] > 0)][['timestamp', c]]

               for c in cols}


查看完整回答
反對 回復(fù) 2021-04-13
  • 3 回答
  • 0 關(guān)注
  • 178 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

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