I have a dataframe with 4 columns. The 4th column contains numbers from 1-150 and I would like to caluclate the number of rows that have the same value n (from 1-150) in column 4. As a simplified example:
a = [[1, 2, 3, 1], [4, 3, 4, 1], [2, 7, 8, 1], [3, 3, 2, 2], [2, 3, 3, 3], [4 ,5, 5, 3]]
should return me a list
a_new = [3, 1, 2]
How can I do this?
CodePudding user response:
you can do a value_counts
, then reindex
and fillna
to add back values that don't exist, and finally tolist
.
df['4th_column'].value_counts().reindex(list(range(1,151))).fillna(0).tolist()