Concept Flow - Creating new columns
Start with DataFrame
Define new column values
Assign new column to DataFrame
DataFrame updated with new column
End
We start with a DataFrame, define values for a new column, assign it, and update the DataFrame.
import pandas as pd df = pd.DataFrame({'A': [1, 2, 3]}) df['B'] = df['A'] * 2 print(df)
| Step | Action | DataFrame State | New Column 'B' Values |
|---|---|---|---|
| 1 | Create DataFrame with column 'A' | {'A': [1, 2, 3]} | N/A |
| 2 | Calculate new column 'B' as df['A'] * 2 | Same as step 1 | [2, 4, 6] |
| 3 | Assign new column 'B' to DataFrame | {'A': [1, 2, 3], 'B': [2, 4, 6]} | [2, 4, 6] |
| 4 | Print DataFrame | Displays both columns | [2, 4, 6] |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | Final |
|---|---|---|---|---|---|
| df | undefined | {'A': [1, 2, 3]} | {'A': [1, 2, 3]} | {'A': [1, 2, 3], 'B': [2, 4, 6]} | {'A': [1, 2, 3], 'B': [2, 4, 6]} |
| df['B'] | undefined | undefined | [2, 4, 6] | [2, 4, 6] | [2, 4, 6] |
Creating new columns in pandas: - Use df['new_col'] = values - Values can be Series, list, or calculations - Length of values must match DataFrame rows - New column appears after assignment - Useful for adding derived data