0

I need help using pandas to group data. The data looks something like this:

12233: 5
12332: 5
12322: 5
12311: 4
22211: 4
12212: 3

Instead of it showing the 3 different numbers appearing 5 times, I would like it to just write how many unique numbers appear 5 times if that makes sense. So for this example, it would write that 3 unique numbers appeared 5 times and 2 unique numbers appeared 4 times. Any help would be appreciated.

How I would like the data to look:

5 : 3
4: 2
3: 1

Here is the code I currently have:

from collections import Counter
import time
import pandas as pd
filename = input("Enter name to save this file as:\n\n")
print("Your file will be saved as: " + filename + ".csv\n\n")
time.sleep(0.5)
print("Please copy/paste numbers into here.")
contents = []


def amount():
    while True:
        try:
            line = input()

        except EOFError:
            break
        contents.append(line)

    return


amount()


count = Counter(contents)
print(count)

d = count
df = pd.DataFrame.from_dict(d, orient='index').reset_index()
df.columns = ['column1', 'column2']

df = df.sort_values(by=['column2'], ascending=False)

print(df)
df.to_csv(filename + ".csv", encoding='utf-8', index=False)
Camol1
  • 35
  • 6
  • Your question is unclear. Can you please [edit] to include a [mcve] showing sample input, expected output based on that input, and code for what you've tried based on your own research? See [How to make good pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) for help on making sample i/o – G. Anderson Oct 19 '22 at 19:10
  • can you also share the dataframe constructor code? what you shared here are these two columns or one column. – Naveed Oct 19 '22 at 19:11
  • @G.Anderson I just revised everything. – Camol1 Oct 19 '22 at 19:14
  • This looks like it may be as simple as `df['column2'].value_counts()` – G. Anderson Oct 19 '22 at 19:16

0 Answers0