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']
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
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)
3条答案
按热度按时间mbjcgjjk1#
试试这个:
输出:
使用
isin()
是一个非常强大的工具,可以满足您的需求。它接受一个列表,如果值在列表中,则返回True
。如果您想要整个 Dataframe ,只需删除末尾的['Country]
:如果需要实际值,可以使用
pd.series.values
izkcnapc2#
如果您只需要相应交叉点处的值:
dxxyhpgq3#
所以这对我来说很管用。