I understand there is a way to create case expressions in SQL based on current columns. Is there a way to do this in Python with Pandas?
The goal is for Python to read an Excel file and create new columns based on conditions.
The query would start like this:
import pandas as pd
df1 = pd.read_excel('data.xlsx')
in SQL it would be read as follows:
Case
When Type = 'Banana%' THEN 'Fruit'
End
Case
When Type = 'Apple%' THEN 'Fruit'
End
Case
When Type = 'Carrot' THEN 'Vegetable'
End
The data in Excel looks like this:
Type |
---|
Apple |
Banana |
Carrot |