Tomer
Tomer

Reputation: 143

Python how to create new dataset from an existing one based on condition

For example: I have this code:

import pandas
df = pandas.read_csv('covid_19_data.csv')

this dataset has a column called countryterritoryCode which is the country code of the country.sample data from the dataset

This dataset has information about covid19 cases from all the countries in the world. How do I create a new dataset where only the USA info appears (where countryterritoryCode == USA)

Upvotes: 0

Views: 2067

Answers (2)

vagitus
vagitus

Reputation: 486

Use df.groupby:

df = pandas.read_csv('covid_19_data.csv')

df_new = df.groupby('countryterritoryCode', axis = 1)

Upvotes: 0

Benjamin
Benjamin

Reputation: 3448

import pandas
df = pandas.read_csv('covid_19_data.csv')
new_df = df[df["country"] == "USA"]
or
new_df = df[df.country == "USA"]

Upvotes: 3

Related Questions