This is my sample csv
JavaScript
x
9
1
ID DESC
2
1 1 Car
3
2 2 Boat
4
3 3 Bike
5
4 4 Car
6
5 5 Car
7
6 6 Car
8
7 7 Car
9
When I do .value_counts()
I get
JavaScript
1
4
1
5
2
1
3
1
4
I want to get
JavaScript
1
5
1
ID DESC COUNT
2
1 Car 5
3
2 Boat 1
4
3 Bike 1
5
This is my current attempt
JavaScript
1
22
22
1
import pandas as pd
2
3
path = "C:Matching_Desc.csv"
4
5
df = pd.read_csv(path, encoding= 'unicode_escape')
6
7
df2 = df['DESC'].value_counts().index.tolist()
8
9
df3 = df['DESC'].value_counts()
10
11
df2 = pd.DataFrame(df2)
12
df3 = pd.DataFrame(df3)
13
14
df3.columns = ['COUNT']
15
df2.columns = ['DESC']
16
17
frames = [df2,df3]
18
19
result = pd.concat(frames)
20
21
result.to_csv("C:matching.csv", index=False)
22
This does not concat the two df properly and does not have the ID
Any suggestions?
Advertisement
Answer
You can use a groupby.agg
in place of value_counts
:
JavaScript
1
6
1
(df.groupby('DESC', as_index=False, sort=False)
2
.agg(**{'ID': ('ID', 'first'),
3
'COUNT': ('ID', 'size')
4
})
5
)
6
Output:
JavaScript
1
6
1
DESC ID COUNT
2
0 Car 1 5
3
1 Boat 2 1
4
2 Bike 3 1
5
6