I want to create a pandas framework with two columns, the first of which is the unique values ββof one of my columns, and the second is the number of unique values.
I saw a lot of posts (such here ) that describe how to get the counts, but the problem I am facing is when I try to create a dataframe, the column values ββbecome my index.
Example data: df = pd.DataFrame({'Color': ['Red', 'Red', 'Blue'], 'State': ['MA', 'PA', 'PA']}) . I want to end up with a dataframe like:
Color Count 0 Red 2 1 Blue 1
I tried the following, but in all cases the index ends with βColorβ and the graph is the only column in the data frame.
Attempt 1:
df2 = pd.DataFrame(data=df['Color'].value_counts())
Attempt 2:
df3 = df['Color'].value_counts() df3 = pd.DataFrame(data=df3, index=range(df3.shape[0]))
Attempt 3:
df4 = df.groupby('Color') df4 = pd.DataFrame(df4['Color'].count())
source share