repl.it
@menezesenderson/

Question: 59090348

Python

https://stackoverflow.com/questions/59090348/how-can-i-count-the-values-in-the-same-column/59090821#59090821

fork
loading
Files
  • main.py
  • test_input.txt
  • Packager files
  • poetry.lock
  • pyproject.toml
  • requirements.txt
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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)

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

dataframe = dataframe.rename(columns={0:'unique'})

print(dataframe)

dataframe = dataframe.sort_values(by=['unique'])

print(dataframe)
Fetching token
?