在pandas中,axis=0代表行,axis=1代表列。因此,要获取 pandas 中每行值的总和,请调用 df.sum(axis=0) 。但它返回每列中的值的总和,反之亦然。为什么???
import pandas as pd
df=pd.DataFrame({"x":[1,2,3,4,5],"y":[2,4,6,8,10]})
df.sum(axis=0)
Run Code Online (Sandbox Code Playgroud)
数据框:
x y
0 1 2
1 2 4
2 3 6
3 4 8
4 5 10
Run Code Online (Sandbox Code Playgroud)
输出:
x 15
y 30
Run Code Online (Sandbox Code Playgroud)
预期输出:
0 3
1 6
2 9
3 12
4 15
Run Code Online (Sandbox Code Playgroud)