久久久久久久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 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 午夜电影一区二区 | 亚洲国产精品久久久 | 久久av一区二区三区 | 国产中的精品av涩差av | 亚洲欧美在线观看 | 中文在线一区二区 | 成人午夜毛片 | 国产视频一区二区 | 91视频官网 | 亚洲日日夜夜 | 欧美一级黄带 | 成人免费淫片aa视频免费 | 99久久婷婷国产综合精品首页 | 日韩成人在线观看 | 亚洲视频一区在线播放 | 国产91久久精品一区二区 | 国产精品一区二区福利视频 | 国产九九av| 成人精品一区 | 欧美aa在线 | 亚洲一区二区三区在线播放 | 久久这里有精品 | 青青久草| 天堂久久天堂综合色 | av网站在线播放 | 欧美精品在线免费观看 | 日本黄色短片 | 成人区一区二区三区 | 久久久美女 | 国产 欧美 日韩 一区 | 免费久久精品 | 国产 日韩 欧美 中文 在线播放 | 国内自拍偷拍一区 | 久久精品视频在线观看 | 成人精品区| 国产永久免费 | 男女网站免费 | 亚洲欧美一区二区三区视频 | 亚洲在线视频 | 亚洲天堂久久 | www.伊人.com |