How would I go about looking up a specific value in a column, then selecting a subset of the column values in that row in pandas?
I have a CSV with a column full of US state names and columns with attributes about each state, but I only want certain values about the states I'm looking up.
For example, there are 50 rows (for the 50 states) and 20 columns with various data about each state, and I want to select Colorado and Florida and only 5 of the column values for those states.
Here is my code that I want to modify:
import glob
import pandas as pd
import os
import csv
myList = []
path = "/path/to/source/files/*.csv"
for fname in glob.glob(path):
df = pd.read_csv(fname)
row = df.loc[df['Province_State'] == 'Pennsylvania']
# Put the date in, derived from the CSV name
dateFromFilename = os.path.basename(fname).replace('.csv','')
row['Date'] = dateFromFilename
myList.append(row)
print(row)
concatList = pd.concat(myList, sort=True)
concatList.to_csv('/path/to/output.csv', index=False, header=True)