Posts

Pandas DataFrames: Create new rows with calculations across existing rows

Image
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box; } 6 How can I create new rows from an existing DataFrame by grouping by certain fields (in the example "Country" and "Industry") and applying some math to another field (in the example "Field" and "Value")? Source DataFrame df = pd.DataFrame({'Country': ['USA','USA','USA','USA','USA','USA','Canada','Canada'], 'Industry': ['Finance', 'Finance', 'Retail', 'Retail', 'Energy', 'Energy', ...