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

Pandas-通過對列和索引的值求和來合并兩個數據框

Pandas- merging two dataframe by sum the values of columns and index(Pandas-通過對列和索引的值求和來合并兩個數據框)
本文介紹了Pandas-通過對列和索引的值求和來合并兩個數據框的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我想按索引和列合并兩個數據集.

I want to merge two datasets by indexes and columns.

我想合并整個數據集

df1 = pd.DataFrame([[1, 0, 0], [0, 2, 0], [0, 0, 3]],columns=[1, 2, 3])
df1
    1   2   3
0   1   0   0
1   0   2   0
2   0   0   3

df2 = pd.DataFrame([[0, 0, 1], [0, 2, 0], [3, 0, 0]],columns=[1, 2, 3])
df2
    1   2   3
0   0   0   1
1   0   2   0
2   3   0   0

我已經嘗試過這段代碼,但我得到了這個錯誤.我不明白為什么它將軸的大小顯示為錯誤.

I have tried this code but I got this error. I can't get why it shows the size of axis as an error.

df_sum = pd.concat([df1, df2])
       .groupby(df2.index)[df2.columns]
       .sum().reset_index()

ValueError: Grouper and axis must be same length

這就是我預期的 df_sum 的輸出

This was what I expected the output of df_sum

df_sum
    1   2   3
0   1   0   1
1   0   4   0
2   3   0   3

推薦答案

你可以使用:df1.add(df2, fill_value=0).它會將 df2 添加到 df1 中,并且它會將 NAN 值替換為 0.

You can use :df1.add(df2, fill_value=0). It will add df2 into df1 also it will replace NAN value with 0.

>>> import numpy as np
>>> import pandas as pd
>>> df2 = pd.DataFrame([(10,9),(8,4),(7,np.nan)], columns=['a','b'])
>>> df1 = pd.DataFrame([(1,2),(3,4),(5,6)], columns=['a','b'])
>>> df1.add(df2, fill_value=0)

    a     b
0  11  11.0
1  11   8.0
2  12   6.0

這篇關于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 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 欧美xxxx性 | 欧美福利在线 | 日韩午夜片 | 成人午夜激情 | 久久婷婷色| 女子spa高潮呻吟抽搐 | 国产盗摄一区二区 | 在线色网站 | 国产3p视频| 国产精品一区二区在线播放 | 91亚洲国产成人精品性色 | 夜夜狠狠擅视频 | 久久h| 国产中文字幕在线 | 久久黄色影院 | 欧美综合在线视频 | 日韩精品一区在线 | 日韩av手机在线 | 国产一区免费视频 | 999毛片| 黄色av免费| 四虎影院最新地址 | 色综合久久88 | 免费一级黄色 | aaa级片| a视频在线免费观看 | 三上悠亚激情av一区二区三区 | 偷偷操不一样 | 日韩在线综合 | 在线观看二区 | 日韩精品视频在线 | 福利视频网址导航 | 伊人久久网站 | 成人久久网 | 国产精品一区二区在线播放 | 亚洲我射av | 欧美色图一区二区三区 | 欧美美女一区二区 | 日本中文字幕视频 | 福利视频一区二区 | 97精品超碰一区二区三区 |