![]() |
VOOZH | about |
In Pandas you can create a new column by iterating over an existing column (a Series) with a for loop, collecting computed values into a Python list and assigning that list back to the DataFrame as a new column.
For Example: This example creates a new column that doubles the numbers in another column.
Number Double 0 1 2 1 2 4 2 3 6
Explanation:
Let's see more examples to understand it better.
Example 1: This example checks each voter’s age and creates a new column "Voter" showing whether the person is eligible to vote.
Output
Voter_name Voter_age Voter
0 Geek1 15.0 No
1 Geek2 23.0 Yes
2 Geek3 25.0 Yes
3 Geek4 9.0 No
4 Geek5 67.0 Yes
5 Geek6 54.0 Yes
6 Geek7 42.0 Yes
7 Geek8 NaN Not Sure
Explanation:
Example 2: In this example, we classify salaries into "High" or "Low" categories and store them in a new column.
Employee Salary Category 0 A 25000 Low 1 B 60000 High 2 C 40000 Low 3 D 80000 High
Explanation: