Modify Columns - Problem
DataFrame Salary Modification

You are working as a data analyst for a company that has decided to give all employees a 100% salary increase due to exceptional performance this year! ๐ŸŽ‰

Given a DataFrame employees with columns name and salary, you need to modify the existing salary column by multiplying each employee's current salary by 2.

Input: A pandas DataFrame with employee names and their current salaries
Output: The same DataFrame with all salaries doubled

Note: This is a DataFrame manipulation problem focusing on column modification operations.

Input & Output

example_1.py โ€” Basic Salary Update
$ Input: employees = pd.DataFrame({'name': ['Alice', 'Bob'], 'salary': [50000, 60000]})
โ€บ Output: pd.DataFrame({'name': ['Alice', 'Bob'], 'salary': [100000, 120000]})
๐Ÿ’ก Note: Each employee's salary is doubled: Alice goes from $50,000 to $100,000, and Bob goes from $60,000 to $120,000
example_2.py โ€” Single Employee
$ Input: employees = pd.DataFrame({'name': ['Charlie'], 'salary': [75000]})
โ€บ Output: pd.DataFrame({'name': ['Charlie'], 'salary': [150000]})
๐Ÿ’ก Note: With only one employee, Charlie's salary is doubled from $75,000 to $150,000
example_3.py โ€” Multiple Employees
$ Input: employees = pd.DataFrame({'name': ['Alice', 'Bob', 'Charlie', 'David'], 'salary': [50000, 60000, 70000, 80000]})
โ€บ Output: pd.DataFrame({'name': ['Alice', 'Bob', 'Charlie', 'David'], 'salary': [100000, 120000, 140000, 160000]})
๐Ÿ’ก Note: All four employees receive the salary doubling: 50kโ†’100k, 60kโ†’120k, 70kโ†’140k, 80kโ†’160k

Constraints

  • 1 โ‰ค number of employees โ‰ค 1000
  • 1 โ‰ค salary โ‰ค 106
  • All salaries are positive integers
  • Employee names are non-empty strings

Visualization

Tap to expand
DataFrame Salary Modification VisualizationStep 1: Original DataName | SalaryAlice | 50000Bob | 60000Charlie | 70000David | 80000Step 2: Select ColumnSalary Column:50000600007000080000Step 3: Apply ร— 2Vectorized Op:50000 ร— 2 = 10000060000 ร— 2 = 12000070000 ร— 2 = 14000080000 ร— 2 = 160000ร—2Step 4: Final ResultName | New SalaryAlice | 100000Bob | 120000Charlie | 140000David | 160000Code Implementationemployees['salary'] = employees['salary'] * 2โœจ Pandas vectorization handles all rows simultaneously๐ŸŽฏ All employees get their well-deserved raise! ๐ŸŽ‰
Understanding the Visualization
1
Access Column
Select the entire salary column from the DataFrame
2
Apply Operation
Use vectorized multiplication to double all values at once
3
Update DataFrame
The modified column replaces the original salary column
4
Return Result
Return the DataFrame with all salaries successfully doubled
Key Takeaway
๐ŸŽฏ Key Insight: Vectorized operations in pandas allow us to perform mathematical operations on entire columns simultaneously, making data transformations both fast and intuitive.
Asked in
Netflix 25 Spotify 20 Airbnb 15 Uber 12
28.5K Views
High Frequency
~5 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen