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

pandas :將多列匯總為一列,沒有最后一列

Pandas: sum up multiple columns into one column without last column( pandas :將多列匯總為一列,沒有最后一列)
本文介紹了 pandas :將多列匯總為一列,沒有最后一列的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

如果我有一個類似于這個的數據框

If I have a dataframe similar to this one

Apples   Bananas   Grapes   Kiwis
2        3         nan      1
1        3         7        nan
nan      nan       2        3

我想添加這樣的列

Apples   Bananas   Grapes   Kiwis   Fruit Total
2        3         nan      1        6
1        3         7        nan      11
nan      nan       2        3        5

我猜你可以使用 df['Apples'] + df['Bananas'] 等等,但我的實際數據框比這大得多.我希望像 df['Fruit Total']=df[-4:-1].sum 這樣的公式可以在一行代碼中解決問題.然而這并沒有奏效.有沒有辦法在不明確總結所有列的情況下做到這一點?

I guess you could use df['Apples'] + df['Bananas'] and so on, but my actual dataframe is much larger than this. I was hoping a formula like df['Fruit Total']=df[-4:-1].sum could do the trick in one line of code. That didn't work however. Is there any way to do it without explicitly summing up all columns?

推薦答案

可以先通過 iloc 然后是 sum:

You can first select by iloc and then sum:

df['Fruit Total']= df.iloc[:, -4:-1].sum(axis=1)
print (df)
   Apples  Bananas  Grapes  Kiwis  Fruit Total
0     2.0      3.0     NaN    1.0          5.0
1     1.0      3.0     7.0    NaN         11.0
2     NaN      NaN     2.0    3.0          2.0

所有列的總和使用:

df['Fruit Total']= df.sum(axis=1)

這篇關于 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 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 一区二区三区高清 | 日韩在线精品视频 | 欧美一区二区三区视频在线播放 | 成人av免费看 | 午夜在线| 色www精品视频在线观看 | 国产精品视频免费看 | 欧美一级在线观看 | 成人不卡视频 | 中文字幕一区二区三区四区 | 91网在线播放 | 欧美日韩国产免费 | 国产精久久久 | 欧美中文字幕一区二区三区 | 久久综合九九 | 免费v片在线观看 | 亚洲精选久久 | 一级a性色生活片久久毛片 一级特黄a大片 | 国产一区二区在线免费观看 | 天天干天天操 | 国产精品成人一区二区三区 | 亚洲第一天堂 | 韩日一区 | 精品在线观看一区二区 | 国产精品久久影院 | 精品久久久久久久久久久久久久 | 国产毛片毛片 | 狠狠久 | 亚洲综合大片69999 | 成人妇女免费播放久久久 | 欧美三区视频 | 久久久久久国模大尺度人体 | 在线一级片 | 日本亚洲一区 | 午夜不卡福利视频 | 欧美a级成人淫片免费看 | 黑人精品欧美一区二区蜜桃 | 97伦理 | 国产综合久久久久久鬼色 | 日韩欧美天堂 | 亚洲精品在线国产 |