Python Pandas sum of dataframe with one column(Python Pandas 数据框总和与一列)
问题描述
我有一个 Python Pandas 数据框:
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
问题:我想将第一列添加到整个数据框中.我想得到这个:
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.
尝试:我试过这个:
print df.add(df.iloc[:,0], fill_value=0)
但它不起作用.它返回错误消息:
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
>>>
这篇关于Python Pandas 数据框总和与一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python Pandas 数据框总和与一列
- python-m http.server 443--使用SSL? 2022-01-01
- 分析异常:路径不存在:dbfs:/databricks/python/lib/python3.7/site-packages/sampleFolder/data; 2022-01-01
- pytorch 中的自适应池是如何工作的? 2022-07-12
- 使用Heroku上托管的Selenium登录Instagram时,找不到元素';用户名'; 2022-01-01
- 如何在 python3 中将 OrderedDict 转换为常规字典 2022-01-01
- padding='same' 转换为 PyTorch padding=# 2022-01-01
- python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令 2022-01-01
- 沿轴计算直方图 2022-01-01
- 如何将一个类的函数分成多个文件? 2022-01-01
- 如何在 Python 的元组列表中对每个元组中的第一个值求和? 2022-01-01