Pandas
Here are all the snippets available using this library:
export_to_csv.py
import pandas as pd
def export_to_csv():
data = {
"name": ["Jon Doe", "Anna Smith", "Peter Jones", "James Smith"],
"age": [32, 34, 29, 42],
"country": ["USA", "Canada", "UK", "USA"],
}
df = pd.DataFrame(data)
df.to_csv("output.csv", index=False)
people.csv
First Name,Last Name,Country,Age
"Bob","Smith","United States",24
"Alice","Williams","Canada",23
"Malcolm","Jone","England",22
"Felix","Brown","United States",23
"Alex","Cooper","Poland",23
"Tod","Campbell","United States",22
"Derek","Ward","Switzerland",25
query_csv_data.py
import pandas as pd
def query_people_from_country(country):
df = pd.read_csv("people.csv")
people_from_united_states = df.query(f"Country == '{country}'")
return people_from_united_states