久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

更新 pandas 中滿足特定條件的行值

Update row values where certain condition is met in pandas(更新 pandas 中滿足特定條件的行值)
本文介紹了更新 pandas 中滿足特定條件的行值的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

假設我有以下數據框:

更新 featanother_feat 列的值的最有效方法是什么/strong>?

What is the most efficient way to update the values of the columns feat and another_feat where the stream is number 2?

是這個嗎?

for index, row in df.iterrows():
    if df1.loc[index,'stream'] == 2:
       # do something

更新:如果我有超過 100 列怎么辦?我不想明確命名要更新的列.我想將每列的值除以 2(流列除外).

UPDATE: What to do if I have more than a 100 columns? I don't want to explicitly name the columns that I want to update. I want to divide the value of each column by 2 (except for the stream column).

所以要明確我的目標是什么:

So to be clear what my goal is:

將所有具有流 2 的行的所有值除以 2,但不更改流列

推薦答案

我覺得你可以使用loc 如果您需要將兩列更新為相同的值:

I think you can use loc if you need update two columns to same value:

df1.loc[df1['stream'] == 2, ['feat','another_feat']] = 'aaaa'
print df1
   stream        feat another_feat
a       1  some_value   some_value
b       2        aaaa         aaaa
c       2        aaaa         aaaa
d       3  some_value   some_value

如果您需要單獨更新,一個選項是使用:

If you need update separate, one option is use:

df1.loc[df1['stream'] == 2, 'feat'] = 10
print df1
   stream        feat another_feat
a       1  some_value   some_value
b       2          10   some_value
c       2          10   some_value
d       3  some_value   some_value

另一個常見的選項是使用 numpy.where:

Another common option is use numpy.where:

df1['feat'] = np.where(df1['stream'] == 2, 10,20)
print df1
   stream  feat another_feat
a       1    20   some_value
b       2    10   some_value
c       2    10   some_value
d       3    20   some_value

如果您需要在條件為 True 的情況下劃分所有不帶 stream 的列,請使用:

If you need divide all columns without stream where condition is True, use:

print df1
   stream  feat  another_feat
a       1     4             5
b       2     4             5
c       2     2             9
d       3     1             7

#filter columns all without stream
cols = [col for col in df1.columns if col != 'stream']
print cols
['feat', 'another_feat']

df1.loc[df1['stream'] == 2, cols ] = df1 / 2
print df1
   stream  feat  another_feat
a       1   4.0           5.0
b       2   2.0           2.5
c       2   1.0           4.5
d       3   1.0           7.0

如果可以使用多個條件,請使用多個 numpy.在哪里numpy.select:

If working with multiple conditions is possible use multiple numpy.where or numpy.select:

df0 = pd.DataFrame({'Col':[5,0,-6]})

df0['New Col1'] = np.where((df0['Col'] > 0), 'Increasing', 
                          np.where((df0['Col'] < 0), 'Decreasing', 'No Change'))

df0['New Col2'] = np.select([df0['Col'] > 0, df0['Col'] < 0],
                            ['Increasing',  'Decreasing'], 
                            default='No Change')

print (df0)
   Col    New Col1    New Col2
0    5  Increasing  Increasing
1    0   No Change   No Change
2   -6  Decreasing  Decreasing

這篇關于更新 pandas 中滿足特定條件的行值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 亚洲综合二区 | 国产超碰人人爽人人做人人爱 | 97精品国产手机 | 亚洲 中文 欧美 日韩 在线观看 | 国产精品久久久久久久久久久久 | 亚洲成人精品一区 | 国产一区二区精品在线观看 | 午夜电影福利 | 国产精品一区二区三 | 国产精品观看 | 亚洲毛片在线观看 | 日韩av在线一区 | 国产一区二区三区在线 | 在线观看 亚洲 | 99re在线 | 91精品国产乱码久久久久久久久 | 久久成人人人人精品欧 | 国产丝袜一区二区三区免费视频 | 亚洲国产精品成人 | 成av在线 | 91精品国产色综合久久 | 天堂素人约啪 | 亚洲精品99 | 欧美日韩一 | 欧美日日 | 在线视频中文字幕 | 久久中文字幕电影 | 香蕉久久久 | 成人a免费| 日日草天天干 | 国产麻豆乱码精品一区二区三区 | 欧美日韩成人在线观看 | 亚洲国产精品福利 | 国产精品一区二区三 | 久久99久久99精品免视看婷婷 | 伊伊综合网 | 亚洲国产精品一区二区第一页 | 国产在线一区二 | 色性av | 一区二区三区视频在线免费观看 | 99热欧美|