how to assign column name in a dataframe . If that column already has values
Question:
I am calculating the value counts of usa state in a data frame . I have obtained a data frame . I have assigned a column name to the value counts i.e ‘Class_0’ , but how to assign column name i.e state_usa to the 1st column of data frame i.e columns of the state.
df1 = data[data['project_is_approved']==0]
['school_state'].value_counts().rename('Class_0')
df1.head()
Actual-Output-Obtained
CA 2183
TX 1382
FL 1041
NY 1027
NC 738
Name: Class_0, dtype: int64
Output-wanted
State Class_0
CA 2183
TX 1382
FL 1041
NY 1027
NC 738
Answers:
Try:
df1 = data[data['project_is_approved']==0]
['school_state'].value_counts().rename('Class_0').reset_index('State')
From pandas docs you should use pandas.DataFrame.rename to ‘Alter axes labels’, i.e. rename a column in a dataframe.
Hope this helps!
I am calculating the value counts of usa state in a data frame . I have obtained a data frame . I have assigned a column name to the value counts i.e ‘Class_0’ , but how to assign column name i.e state_usa to the 1st column of data frame i.e columns of the state.
df1 = data[data['project_is_approved']==0]
['school_state'].value_counts().rename('Class_0')
df1.head()
Actual-Output-Obtained
CA 2183
TX 1382
FL 1041
NY 1027
NC 738
Name: Class_0, dtype: int64
Output-wanted
State Class_0
CA 2183
TX 1382
FL 1041
NY 1027
NC 738
Try:
df1 = data[data['project_is_approved']==0]
['school_state'].value_counts().rename('Class_0').reset_index('State')
From pandas docs you should use pandas.DataFrame.rename to ‘Alter axes labels’, i.e. rename a column in a dataframe.
Hope this helps!