0
0
Data Analysis Pythondata~5 mins

Adding and removing columns in Data Analysis Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
How do you add a new column to a pandas DataFrame?
You can add a new column by assigning a list, array, or a single value to a new column name, like df['new_column'] = values.
Click to reveal answer
beginner
What method removes a column from a pandas DataFrame?
Use df.drop('column_name', axis=1) to remove a column. Set inplace=True to change the DataFrame directly.
Click to reveal answer
beginner
Can you add a column based on calculations from other columns? How?
Yes. For example, df['new_col'] = df['col1'] + df['col2'] adds a column by summing two existing columns.
Click to reveal answer
beginner
What does axis=1 mean in the context of dropping columns?
axis=1 tells pandas to operate on columns. Without it, pandas assumes rows (axis=0).
Click to reveal answer
intermediate
How to remove multiple columns at once from a DataFrame?
Pass a list of column names to df.drop(['col1', 'col2'], axis=1) to remove multiple columns.
Click to reveal answer
Which code adds a new column 'age' with all values 30 to DataFrame df?
Adf.remove('age')
Bdf['age'] = 30
Cdf.drop('age')
Ddf.add_column('age', 30)
How do you remove the column 'salary' from DataFrame df without changing df itself?
Adf.drop('salary')
Bdf.remove('salary')
Cdf.drop('salary', axis=1)
Ddf.delete('salary')
What happens if you do df['total'] = df['price'] * df['quantity']?
AAdds a new column 'total' with product of 'price' and 'quantity'
BDeletes 'total' column
CRaises an error
DReplaces 'price' column
Which axis value is used to drop rows in pandas?
Aaxis=0
Baxis=1
Caxis=2
Daxis=-1
How to remove columns 'A' and 'B' from DataFrame df in one command?
Adf.remove(['A', 'B'])
Bdf.drop('A', 'B')
Cdf.delete('A', 'B')
Ddf.drop(['A', 'B'], axis=1)
Explain how to add a new column to a pandas DataFrame using existing columns.
Think about creating a new column by combining or calculating from others.
You got /3 concepts.
    Describe the difference between dropping a column with and without inplace=True.
    Consider whether the original DataFrame changes or a new one is returned.
    You got /3 concepts.