lundi 17 septembre 2018

Pass names to a list and use 'contains' for each

I have a dataframe that one of its columns called proj has a sentence in every row and in that sentence a name of a city is mentioned. I want to do an if condition that when a password is being passed a different city's data will be available.

proj
sd_32 New York
eo_31 Lisbon
..

Ex.

x = pd.read_csv(r'C:\Users\user\Desktop\Dataset.csv', sep = ',')
while True:
    passw = input('Password').upper()
    if not passw in ('A','B'):
        print('Try again')
        continue
    else:
        break
if passw == 'A':
    df = x[x['proj'].str.contains('New York')]
    print(df) 
elif passw == 'B':
    df = x[x['proj'].str.contains('Lisbon')]
    print(df)   

How to do this in a more Pythonic way?

I thought about making a list :

city = ['New York','Lisbon','Berlin',..] #unique names of cities

and then pass this in a code that for every individual city, depending the password does an if process like I did but with this idea. How can I proceed with this?

Aucun commentaire:

Enregistrer un commentaire