I want to get the frequency count of strings within a column. One one hand, this is similar to collapsing a dataframe to a set of rows that only reflects the strings in the column. I was able to solve this with a loop, but know there is a better solution.
Example df:
2017-08-09 2017-08-10
id
0 pre pre
2 active_1-3 active_1
3 active_1 active_1
4 active_3-7 active_3-7
5 active_1 active_1
And want to get out:
2017-08-09 2017-08-10
pre 1 1
active_1 2 3
active_1-3 3 0
active_3-7 1 1
I searched a lot of forums but couldnt' find a good answer.
I'm assuming a pivot_table approach is the right one, but couldn't get the right arguments to collapse a table that didn't have an obvious index for the output df.
I was able to get this to work by iterating over each column, using value_counts(), and appending each value count series into a new dataframe, but I know there is a better solution.
for i in range(len(date_cols)):
new_values = df[date_cols[i]].value_counts()
output_df = pd.concat([output_df , new_values], axis=1)
Thanks!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…