Created
October 21, 2022 11:40
-
-
Save datagy/64fc5e128fa8b32171e0e23d23045fc6 to your computer and use it in GitHub Desktop.
Adding a total row
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pandas as pd | |
| df = pd.DataFrame({ | |
| 'Name': ['Nik', 'Kate', 'Evan'], | |
| 'Location': ['Toronto, ON', 'Atlanta, GA', 'Portland, OR'], | |
| 'Total': [99.99, 125.65, 33.43] | |
| }) | |
| df['Over 100'] = ['Yes' if Total > 100 else 'No' for Total in df['Total']] | |
| df[['City', 'State']] = df['Location'].str.split(', ', expand=True) | |
| df.loc[3, 'Name'] = 'Total' | |
| df.loc[3, 'Total'] = df['Total'].sum() | |
| print(df) | |
| # Returns: | |
| # Name Location Total Over 100 City State | |
| # 0 Nik Toronto, ON 99.99 No Toronto ON | |
| # 1 Kate Atlanta, GA 125.65 Yes Atlanta GA | |
| # 2 Evan Portland, OR 33.43 No Portland OR | |
| # 3 Total NaN 259.07 NaN NaN NaN |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment