循环循环与组的列
我有此数据集,
age salary gender
44 3000 M
32 4555 F
45 6000 M
50 4200 F
43 5000 F
23 1700 M
我想通过使用Numby进行性别循环,并获得年龄/薪金组的最大/最小值,我做到了:
import pandas as pd
import numby as np
data = pd.read_excel("file")
var = ["age","salary","gender"]
dat = data[var]
column_list = dat.columns.values.tolist()
resAll = pd.DataFrame()
for col in column_list:
minn = np.min(dat[col])
maxx = np.max(dat[col])
res = [[str(col), ' '],
['min', str(minn)],
['max',str(maxx)]
]
resNum = pd.DataFrame(res)
resNum.columns = ['0','1']
print(resNum)
输出
0 1
age
max 50
min 23
salary
max 6000
min 1700
gender
max f
min m
我希望能够按性别组成!有帮助吗?这样,知道列的数量不是固定的大小。
value male female
age
max 45 50
min 23 32
salary
max 6000 5000
min 1700 4200
I have this data set
age salary gender
44 3000 M
32 4555 F
45 6000 M
50 4200 F
43 5000 F
23 1700 M
I want to loop through each column and get max/min value for age/salary group by gender using numby, i did this:
import pandas as pd
import numby as np
data = pd.read_excel("file")
var = ["age","salary","gender"]
dat = data[var]
column_list = dat.columns.values.tolist()
resAll = pd.DataFrame()
for col in column_list:
minn = np.min(dat[col])
maxx = np.max(dat[col])
res = [[str(col), ' '],
['min', str(minn)],
['max',str(maxx)]
]
resNum = pd.DataFrame(res)
resNum.columns = ['0','1']
print(resNum)
output
0 1
age
max 50
min 23
salary
max 6000
min 1700
gender
max f
min m
I want to be able to do that grouped by gender! any help ? like this, knowing that number of columns is not fixed size.
value male female
age
max 45 50
min 23 32
salary
max 6000 5000
min 1700 4200
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
数据框有一个方法
Dricest()
,该可输出每列的描述性统计信息。可以在按列分组后使用它来分解每个组的统计数据。如果您只需要最小值和最大值,则需要删除其余的统计数据。如果您只想保持最小并最大放下剩余的时间,请使用掉落。
dataframes have a method
describe()
which outputs the descriptive statistics for each column. It can be used after grouping by a column to break down the stats for each group. If you only need the min and max, you need to drop the rest of stats.if you want to keep just the min and max and drop the rest, use drop.