Pandas:对随机数行求和
Pandas: sum rows of random numbers
我有以下代码生成随机整数的 100x100 数据帧:
import pandas as pd
import numpy as np
bin = []
cols = []
for i in range(1,101):
cols.append("count_" + str(i))
bin.append(i)
df = pd.DataFrame(np.random.randint(1,10,(100, 100)), index=bin, columns=cols)
count_1 count_2 count_3 ... count_100
1 1 3 6 ... 9
2 2 8 4 ... 7
... ... ... ... ... ...
100 6 3 8 ... 2
如果我想要列的总和,我可以使用:
df.sum()
给我
count_1 516
count_2 505
count_3 503
...
count_98 534
count_99 489
count_100 521
但我想要行的总和。我该如何实现?
DataFrame.sum(axis=None, skipna=None, level=None, numeric_only=None, min_count=0, **kwargs)
Return the sum of the values over the
requested axis.
This is equivalent to the method numpy.sum.
Parameters axis{index (0), columns (1)}
: Axis for the function to be
applied on.
bin = []
cols = []
for i in range(1,21):
cols.append("count_" + str(i))
bin.append(i)
df = pd.DataFrame(np.random.randint(1,10,(20, 20)), index=bin, columns=cols)
for col in cols:
df[col]=df[col].astype(int)
print(df.head())
fig,ax =plt.subplots(figsize=(16,8))
for item in df[cols].sum():
print(item)
grouped=df[cols].sum().plot(kind='barh')
###########how to sum each row###########
df['sum']=0
for key,item in df.iterrows():
sum=0
for col in cols:
sum+=df.loc[key,col]
df.loc[key,'sum']=sum
print (df.head())
我有以下代码生成随机整数的 100x100 数据帧:
import pandas as pd
import numpy as np
bin = []
cols = []
for i in range(1,101):
cols.append("count_" + str(i))
bin.append(i)
df = pd.DataFrame(np.random.randint(1,10,(100, 100)), index=bin, columns=cols)
count_1 count_2 count_3 ... count_100
1 1 3 6 ... 9
2 2 8 4 ... 7
... ... ... ... ... ...
100 6 3 8 ... 2
如果我想要列的总和,我可以使用:
df.sum()
给我
count_1 516
count_2 505
count_3 503
...
count_98 534
count_99 489
count_100 521
但我想要行的总和。我该如何实现?
DataFrame.sum(axis=None, skipna=None, level=None, numeric_only=None, min_count=0, **kwargs)
Return the sum of the values over the requested axis.
This is equivalent to the method numpy.sum.
Parameters
axis{index (0), columns (1)}
: Axis for the function to be applied on.
bin = []
cols = []
for i in range(1,21):
cols.append("count_" + str(i))
bin.append(i)
df = pd.DataFrame(np.random.randint(1,10,(20, 20)), index=bin, columns=cols)
for col in cols:
df[col]=df[col].astype(int)
print(df.head())
fig,ax =plt.subplots(figsize=(16,8))
for item in df[cols].sum():
print(item)
grouped=df[cols].sum().plot(kind='barh')
###########how to sum each row###########
df['sum']=0
for key,item in df.iterrows():
sum=0
for col in cols:
sum+=df.loc[key,col]
df.loc[key,'sum']=sum
print (df.head())