Created
October 21, 2022 11:28
-
-
Save datagy/a4cf6dca4d4999f82a415ef98cfff5af to your computer and use it in GitHub Desktop.
Replicating Text to Columns
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
| # Using text-to-columns in Pandas | |
| 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) | |
| 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment