there series called location_ratings
below :
location_ratings = location['location'].value_counts()
below location_ratings's sample output:
location brazil 180 alaska 175 russia 171 colombia 146 canada 144 california 142 france 130 england 104 india 97 indonesia 84 china 83
there 2 values 1 location , other 1 numeric value (ratings).
i want separate them 2 new columns, 1 should 'location' other should 'ratings' in dataframe.
i tried converting series dataframe , resetting index using below code failed expected result.
failed attempt 1 :
d1 = location_ratings.to_frame().reset_index().t
failed attempt 2 :
d1 = location_ratings.to_frame(). d1.columns = ['location', 'ratings']
you can first change index name rename_axis
(new in pandas
0.18.0
) , reset_index
:
location = pd.dataframe({'location': {0: 'brazil', 1: 'brazil', 2: 'brazil', 3: 'brazil', 4: 'brazil', 5: 'alaska', 6: 'alaska', 7: 'alaska', 8: 'alaska'}}) print (location) location 0 brazil 1 brazil 2 brazil 3 brazil 4 brazil 5 alaska 6 alaska 7 alaska 8 alaska location_ratings = location['location'].value_counts().rename_axis('location') print (location_ratings) location brazil 5 alaska 4 name: location, dtype: int64 d1 = location_ratings.reset_index(name='ratings') print (d1) location ratings 0 brazil 5 1 alaska 4
another solution assigning new column names:
d1 = location_ratings.reset_index() d1.columns = ['location', 'ratings'] print (d1) location ratings 0 brazil 5 1 alaska 4
Comments
Post a Comment