from
pandas
import
Series,DataFrame
import
pandas as pd
import
numpy as np
python中pandas.DataFrame对行与列求和及添加新行与列示例
df
=
DataFrame(np.random.randn(
4
,
5
), columns
=
[
‘A‘
,
‘B‘
,
‘C‘
,
‘D‘
,
‘E‘
])
df[
‘Col_sum‘
]
=
df.
apply
(
lambda
x: x.
sum
(), axis
=
1
)
df.loc[
‘Row_sum‘
]
=
df.
apply
(
lambda
x: x.
sum
())
时间: 2024-09-27 11:45:25