(Jul-08-2020, 08:38 PM)Truman Wrote: After adding print('-' * 50) columns are aligned. Not sure how this code exactly solved the issueIt's the way you combine
Series
to a DataFrame
.After States will have no index/column only area and pop.
>>> data.columns Index(['area', 'pop'], dtype='object')So to fix this use
reset_index()
and give a name.>>> data = data.reset_index() >>> data.columns Index(['index', 'area', 'pop'], dtype='object') # Rename to a useful name data = data.rename(columns={'index': 'States'}) >>> data.columns Index(['States', 'area', 'pop'], dtype='object') >>> >>> data States area pop 0 California 423967 38332521 1 Texas 695662 26448193 2 New York 141297 19651127 3 Florida 170312 19552860 4 Illinois 149995 12882135There is no problem in doing this way to fix it.
I don't now how data is collect or that you have to combine two
pd.Series
like this.There maybe is a way to make usable DataFrame for raw data,
but using
reset_index()
is a common way to fix thing to a usable DataFrame.