0
0
Data Analysis Pythondata~10 mins

Adding and removing columns in Data Analysis Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Adding and removing columns
Start with DataFrame
Add Column
Check DataFrame
Remove Column
Check DataFrame
End
Start with a DataFrame, add a new column, check the result, then remove a column and check again.
Execution Sample
Data Analysis Python
import pandas as pd

df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
df['C'] = df['A'] + df['B']
df = df.drop('B', axis=1)
print(df)
Create a DataFrame, add a new column 'C' as sum of 'A' and 'B', then remove column 'B' and print the result.
Execution Table
StepActionDataFrame ColumnsDataFrame ContentOutput
1Create DataFrame['A', 'B'][[1, 3], [2, 4]]Initial DataFrame with columns A and B
2Add column 'C' as A+B['A', 'B', 'C'][[1, 3, 4], [2, 4, 6]]Column C added with sums
3Remove column 'B'['A', 'C'][[1, 4], [2, 6]]Column B removed
4Print DataFrame['A', 'C'][[1, 4], [2, 6]]Printed final DataFrame
💡 All steps completed; DataFrame now has columns A and C only.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
df.columns.tolist()N/A['A', 'B']['A', 'B', 'C']['A', 'C']['A', 'C']
df.values.tolist()N/A[[1, 3], [2, 4]][[1, 3, 4], [2, 4, 6]][[1, 4], [2, 6]][[1, 4], [2, 6]]
Key Moments - 2 Insights
Why does the new column 'C' appear after step 2?
Because we assigned df['C'] = df['A'] + df['B'], which creates a new column 'C' with the sum of columns 'A' and 'B' as shown in execution_table row 2.
Why does the DataFrame no longer have column 'B' after step 3?
Because we used df.drop('B', axis=1) which removes column 'B' from the DataFrame, as shown in execution_table row 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what columns does the DataFrame have after step 2?
A['A', 'B', 'C']
B['A', 'C']
C['A', 'B']
D['B', 'C']
💡 Hint
Check the 'DataFrame Columns' column in execution_table row 2.
At which step is column 'B' removed from the DataFrame?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action' and 'DataFrame Columns' in execution_table rows.
If we did not specify axis=1 in df.drop('B', axis=1), what would happen?
AColumn 'B' would still be removed
BAn error would occur
CRow 'B' would be removed instead
DNothing would change
💡 Hint
Refer to pandas drop method requirements for axis parameter.
Concept Snapshot
Adding and removing columns in pandas:
- Add column: df['new_col'] = values
- Remove column: df = df.drop('col_name', axis=1)
- axis=1 means column, axis=0 means row
- Changes update DataFrame structure
- Use print(df) to see changes
Full Transcript
We start with a DataFrame with columns A and B. Then we add a new column C by summing columns A and B. This adds column C to the DataFrame. Next, we remove column B using the drop method with axis=1 to specify column removal. Finally, we print the DataFrame which now has columns A and C only. This shows how to add and remove columns step-by-step.