Last answered:

20 Nov 2022

Posted on:

05 Dec 2021


Exercises - column names assignment


I have a question regarding an exercise in creating a new df and changing its column names. It has been asked before, but there is no answer as of yet:

We are asked to create a new DataFrame based on the data in sales_products_data, but with new column names. Then, we should check the column names for both dfs. Why do they change for both the new df, and for the original sales_products_data, if our code only seems to work on the new df?

Code below:

new_column_names = ['SaleID', 'Country', 'OrderMethod', 'Retailer', 'Line',
       'Type', 'Product', 'YearOfSales', 'Quarter', 'Revenue', 'Quantity',

method 1)

df = pd.DataFrame(sales_products_data,
                  columns = new_column_names)

method 2)

df = pd.DataFrame(sales_products_data)
df.columns = new_column_names

Columns Index for both the new df and the original sales_products_data is:

Index(['SaleID', 'Country', 'OrderMethod', 'Retailer', 'Line', 'Type',
       'Product', 'YearOfSales', 'Quarter', 'Revenue', 'Quantity', 'Margin'],

1 answers ( 0 marked as helpful)
Posted on:

20 Nov 2022



Submit an answer