pythonpandasdataframe

df.head() is returning ValueError: Per-column arrays must each be 1-dimensional


Here is my code:

set_theme_count = sets_df["theme_id"].value_counts()[:-5]

Result: DataFrame Head

Now, I want to rename the columns so I can use pd.merge. So this is what I did:

set_theme_count = pd.DataFrame({'id' :set_theme_count.index, 
                            'set_count': set_theme_count.values})

This code runs successfully, but when I try to do set_theme_count.head(), I am getting this error:

ValueError: Per-column arrays must each be 1-dimensional

I am very confused since the DataFrame gets created successfully, but the .head() throws an error. Also, if there is another way to rename the columns, please let me know.

I am using google collab.


Solution

  • Well, instead of creating a new DataFrame, just renaming the axis works. To do that, here is the code: set_theme_counts.rename_axis('id').reset_index(name='set_count')