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

Python Pandas 數(shù)據(jù)框總和與一列

Python Pandas sum of dataframe with one column(Python Pandas 數(shù)據(jù)框總和與一列)
本文介紹了Python Pandas 數(shù)據(jù)框總和與一列的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我有一個(gè) Python Pandas 數(shù)據(jù)框:

I have a Python Pandas DataFrame:

df = pd.DataFrame(np.random.rand(5,3),columns=list('ABC'))
print df
              A           B           C
0   0.041761178 0.60439116  0.349372206
1   0.820455992 0.245314299 0.635568504
2   0.517482167 0.7257227   0.982969949
3   0.208934899 0.594973111 0.671030326
4   0.651299752 0.617672419 0.948121305

問題:我想將第一列添加到整個(gè)數(shù)據(jù)框中.我想得到這個(gè):

Question: I would like to add the first column to the whole dataframe. I would like to get this:

              A           B           C
0   0.083522356 0.646152338 0.391133384
1   1.640911984 1.065770291 1.456024496
2   1.034964334 1.243204867 1.500452116
3   0.417869798 0.80390801  0.879965225
4   1.302599505 1.268972171 1.599421057

對于第一行:

  • 答:0.04176 + 0.04176 = 0.08352
  • B:0.04176 + 0.60439 = 0.64615

要求:我無法使用其列名引用第一列.例如:df.A 不可接受;df.iloc[:,0] 是可以接受的.

Requirements: I cannot refer to the first column using its column name. eg.: df.A is not acceptable; df.iloc[:,0] is acceptable.

嘗試:我試過這個(gè):

print df.add(df.iloc[:,0], fill_value=0)

但它不起作用.它返回錯(cuò)誤消息:

but it is not working. It returns the error message:

Traceback (most recent call last):
  File "C:test.py", line 20, in <module>
    print df.add(df.iloc[:,0], fill_value=0)
  File "C:python27libsite-packagespandascoreops.py", line 771, in f
    return self._combine_series(other, na_op, fill_value, axis, level)
  File "C:python27libsite-packagespandascoreframe.py", line 2939, in _combine_series
    return self._combine_match_columns(other, func, level=level, fill_value=fill_value)
  File "C:python27libsite-packagespandascoreframe.py", line 2975, in _combine_match_columns
    fill_value)
NotImplementedError: fill_value 0 not supported

是否可以將 DataFrame 的所有列與第一列相加?

Is it possible to take the sum of all columns of a DataFrame with the first column?

推薦答案

這就是你需要做的:

df.add(df.A, axis=0)


Example:
>>> df = pd.DataFrame(np.random.rand(5,3),columns=['A','B','C'])
>>> col_0 = df.columns.tolist()[0]

>>> print df
          A         B         C
0  0.502962  0.093555  0.854267
1  0.165805  0.263960  0.353374
2  0.386777  0.143079  0.063389
3  0.639575  0.269359  0.681811
4  0.874487  0.992425  0.660696
>>> df = df.add(df.col_0, axis=0)
>>> print df
          A         B         C
0  1.005925  0.596517  1.357229
1  0.331611  0.429766  0.519179
2  0.773553  0.529855  0.450165
3  1.279151  0.908934  1.321386
4  1.748975  1.866912  1.535183
>>> 

這篇關(guān)于Python Pandas 數(shù)據(jù)框總和與一列的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區(qū)域周圍繪制一個(gè)矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個(gè)矩形邊界框中應(yīng)用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據(jù)文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 欧美九九九 | 免费久久网站 | 日本免费一区二区三区视频 | 午夜在线视频 | 欧美精品影院 | 国产成人精品免费 | 欧美色综合 | 91在线导航 | 91精品无人区卡一卡二卡三 | 精品久久久久久亚洲精品 | 免费看欧美一级片 | 亚洲欧美日韩久久 | 高清一区二区三区 | 国产亚洲欧美另类一区二区三区 | www.久久久.com | 国产一区二区三区四区五区加勒比 | 请别相信他免费喜剧电影在线观看 | 欧美亚洲国语精品一区二区 | 亚洲一区二区在线 | 91久久北条麻妃一区二区三区 | 国产高清视频在线观看 | 中文字幕一区二区三区不卡在线 | 在线看91| 91影院在线观看 | 日本一区二区三区四区 | 国产精品不卡一区二区三区 | 日日日操| 精品区 | 九九在线精品视频 | 日韩精品在线网站 | 久在线 | 涩涩导航| 欧美高清视频一区 | 欧美一级在线免费观看 | 欧美日高清 | 国产毛片毛片 | 欧美黑人一级爽快片淫片高清 | 亚洲永久 | 中文字幕在线看第二 | 91av在线影院 | 一区二区三区在线免费观看 |