Matching variable value in pandas CSV
Question:
I have a CSV file with a list of Towns, Countys, and Country.
Town County Country
Ampthill Bedfordshire England
Arlesey Bedfordshire England
I want to be able to check if a town is in the list, if a town is in the list then I want to know what the country is.
Currently using pandas.
Any thoughts on how to check if a variable value is present in pandas.
Answers:
try this :
import pandas as pd
df = pd.read_csv('YOUR_PATH_HERE')
# to check if a single town
country = df.loc[(df['Town'] == 'Arlesey')]['Country']
# using the isin() operator
df.loc[df['Town'].isin(['Arlesey'])]['Country']
output:
1 England
using isin()
is a very powerful tool for what you are looking for. It takes a list and returns True
if the value is in the list. If you want the entire dataframe, just remove ['Country]
at the end:
Town County Country
1 Arlesey Bedfordshire England
If you need the actual values, you could use pd.series.values
# using values[0] because it is the first element in the array
df.loc[df['Town'].isin(['Arlesey'])]['Country'].values[0]
If you just want the value at the corresponding intersection:
import pandas as pd
df = pd.read_csv('test.csv')
if 'ampthill' in df['town'].values:
index = df.town[df.town == 'ampthill'].index.tolist()[0]
country = df.loc[index, 'country']
print(country)
>>> england
So this worked for me…
from pathlib import Path
import pandas as pd
home = str(Path.home())
csv_folder = Path(r"TO_CSV_FOLDER")
csv = csv_folder / 'town-county-country.csv'
town = 'Ampthill'
with open(csv,'r') as csv:
ctu = ["Town","County","Country"]
cas = pd.read_csv(csv,usecols=ctu)
index = cas.Town[cas.Town == town].index.tolist()[0]
named = cas.loc[index, 'Country']
print(named)
I have a CSV file with a list of Towns, Countys, and Country.
Town County Country Ampthill Bedfordshire England Arlesey Bedfordshire England
I want to be able to check if a town is in the list, if a town is in the list then I want to know what the country is.
Currently using pandas.
Any thoughts on how to check if a variable value is present in pandas.
try this :
import pandas as pd
df = pd.read_csv('YOUR_PATH_HERE')
# to check if a single town
country = df.loc[(df['Town'] == 'Arlesey')]['Country']
# using the isin() operator
df.loc[df['Town'].isin(['Arlesey'])]['Country']
output:
1 England
using isin()
is a very powerful tool for what you are looking for. It takes a list and returns True
if the value is in the list. If you want the entire dataframe, just remove ['Country]
at the end:
Town County Country
1 Arlesey Bedfordshire England
If you need the actual values, you could use pd.series.values
# using values[0] because it is the first element in the array
df.loc[df['Town'].isin(['Arlesey'])]['Country'].values[0]
If you just want the value at the corresponding intersection:
import pandas as pd
df = pd.read_csv('test.csv')
if 'ampthill' in df['town'].values:
index = df.town[df.town == 'ampthill'].index.tolist()[0]
country = df.loc[index, 'country']
print(country)
>>> england
So this worked for me…
from pathlib import Path
import pandas as pd
home = str(Path.home())
csv_folder = Path(r"TO_CSV_FOLDER")
csv = csv_folder / 'town-county-country.csv'
town = 'Ampthill'
with open(csv,'r') as csv:
ctu = ["Town","County","Country"]
cas = pd.read_csv(csv,usecols=ctu)
index = cas.Town[cas.Town == town].index.tolist()[0]
named = cas.loc[index, 'Country']
print(named)