|
|
发表于 2015-6-4 03:59:43
|
显示全部楼层
上pandas这类东西比较好用,就是学习起来费点劲,这也是边查边写的。
- # coding: utf-8
- __author__ = 'Reed'
- import pandas # __version__ == 0.15.2
- from pandas import DataFrame
- import numpy as np
- with open('fishc.txt') as f:
- df = pandas.read_csv(f, delim_whitespace=True, header=None, names=['id', 'type', 'count', 'sum'])
- df['division'] = df['count'] / df['sum']
- def func(x):
- return np.log10(x.loc[x.type == 2, 'division'] / x.loc[x.type != 2, 'division'].max())
- result = DataFrame(df.groupby('id').apply(func))
- result = result.rename(columns={'division': 'log'})
- print(df)
- print()
- print(result) # if log==0 means type2 is the max division or equal.
复制代码
|
|