计算列的唯一值并存储在新列中

时间:2019-12-06 16:20:03

标签: python pandas

last_col = re.split("\s{2,}", l)[-1] # eg '850 (early) - 1100 (late)' or '600'
patt = re.compile("(?P<num1>[0-9]+)[a-zA-z(\)\- ]+(?P<num2>[0-9]+)")
g = patt.search(last_col)

if g:
    val = (int(g.group('num1')) + int(g.group('num2'))) / 2
else:
    val = int(last_col)

打印# Dummy df. data = [ [11, 'a'], [11, 'b'], [10, 'c'], [9, 'd'], [1, 'e'], [1, 'f'] ] column_name = ['freq', 'recordings'] df = pd.DataFrame(data, columns=column_name)

df

我想看看freq id 11 a 11 b 10 c 9 d 1 e 1 f 的每个值存储了多少次。所需的输出:

freq

1 个答案:

答案 0 :(得分:1)

C:\Program Files\MongoDB\Server\4.2\bin\

解决方案:

data = [
    [11, 'a'],
    [11, 'b'],
    [10, 'c'],
    [9, 'd'],
    [1, 'e'],
    [1, 'f']
]
column_name = ['freq', 'recordings']

df = pd.DataFrame(data, columns=column_name)

输出:

from collections import Counter
Counter(df.freq)