簡體   English   中英

如何有效地更改數據框中的值

[英]How to change values in a dataframe efficiently

# Incorporate delisting return
i = 0
for tc, col in dlret.iloc[:,0:50].iteritems():
    idx = col.index[col.notnull()]
    if len(idx) != 0:
        tr = idx[0]
        val = col.ix[tr]
        #ret.ix[tr, tc] = val #this line is too slow
    i += 1
    if math.floor(i/10) > math.floor((i-1)/10):
        print i

dlret DataFrame大約有600行和25000+列。 我遍歷各列以查找第一個非空值(退市收益),然后在ret DataFrame中找到相應位置,以將值設置為退市收益。 但是,使用ix索引相應位置時,代碼運行速度很慢。 關於如何有效實現這一目標的任何建議?

根據您的評論,您想要的是遍歷各列以查找每列的第一個非空值並更新ret DataFrame。 您可以使用以下代碼執行此操作:

mask_first_nonnull = dlret.notnull() & (dlret.notnull().cumsum()==1)
ret[mask_first_nonnull]=dlret[mask_first_nonnull]   

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM