Create A Dataframe From For Loop Output
I need to create a dataframe from the output of the following for loop. I tried writing a for loop but I do not know how to make the results into a dataframe. I also need to count
Solution 1:
Fix your output
output.sum(level=0)
Or using stack
final_merged.stack().value_counts()
Or numpy way unique
v,c=np.unique(df.values.ravel(),return_counts =True)
pd.Series(c,index=v)Solution 2:
If use value_counts it return unique index, so aggregate sum is not necessary.
For DataFrame use Series.rename_axis with Series.reset_index:
for c in final_merged.columns:
print(final_merged[c].value_counts().rename_axis('activity').reset_index(name='count'))
If need DataFrame from all columns add DataFrame.stack:
df = final_merged.stack().value_counts().rename_axis('activity').reset_index(name='count')
Post a Comment for "Create A Dataframe From For Loop Output"