I have dataframe consisting of more than 200 columns. I want to value_counts() in each column. Below is my code which is working fine but when I want to create "csv". The below code only enter the last column (value count). I want all.
import pandas as pd
df = pd.read_csv("hcp.csv")
for col in df:
df2 = df[col].value_counts()
print(df2)
df2.to_csv("new_hcp.csv")
The print(df2) is showing all value counts but not "CSV". Anyone who can help, I will be grateful.
df2gets overwritten in each iteration of the loop, so you only end up with the last value. Create an empty DF and append rows to it with your values in each iteration, then output that df