简体   繁体   English

如何计算同一列中的值

[英]How can I count the values in the same column

I want to get the statistics of a long column, but I have the problems that in the colomn are diffrent datas( A,B,C,D.. ) and the same values ( 2 ) that I will count.我想获得一长列的统计数据,但我遇到的问题是列中有不同的数据( A、B、C、D.. )和我将计算的相同值( 2 )。

Example:例子:

A
2
2
2
2
B
2
2
C
D
E
2
2

Output will be like:输出将是这样的:

A 4
B 2
C
D
E 2

Check where the Series , s , equals your magic number.检查Seriess在哪里等于您的幻数。 Form groups after masking by that same check, but forward filling.通过相同的检查屏蔽后形成组,但向前填充。

u = s.eq('2')  # `2` if it's not a string
u.groupby(s.mask(u).ffill()).sum()

A    4.0
B    2.0
C    0.0
D    0.0
E    2.0
dtype: float64

Input data:输入数据:

import pandas as pd
s = pd.Series(list('A2222B22CDE22'))

I am assuming that we are working with a text file.我假设我们正在处理一个文本文件。 ('test_input.txt') ('test_input.txt')

import pandas as pd

data = pd.read_csv('test_input.txt', header=None)
data = list(data[0])
final_out = dict()
last_item = None

for item in data:
    try:
        item = int(item)
    except ValueError:
        item = str(item)    

    if isinstance(item, str):
        last_item = item
        final_out[last_item] = 0

    if isinstance(item, int):
        final_out[last_item] += 1    

print(final_out)
## {'A': 4, 'B': 2, 'C': 0, 'D': 0, 'E': 2}

print(pd.DataFrame.from_dict(final_out, orient='index'))

##    0
## A  4
## B  2
## C  0
## D  0
## E  2

# For order column, create first.
dataframe = dataframe.rename(columns={0:'unique'})
print(dataframe)

# Ordering
dataframe = dataframe.sort_values(by=['unique'])
print(dataframe)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM