Q:

How to change values in specific rows depending on given condition in pandas?

I want to do something like this:
for row in df:
if row['Country'] == 'unknown':
    row['Country'] = city2country_mapping[row['city']]

Country and City are columns.
'city2country_mapping' is a dictionary where key:value pair is 'city':'country'.
(basically i'm trying to fill in the unknowns by getting country from dictionary as i know city for each row) 

A:

You can vectorize this with pandas.Series.where:
df['country'] = df.country.where(
    df.country != 'unknown', df.city.map(city2country_mapping))

df.city.map(city2country_mapping) will first create a Series that contains the corresponding country for each city, and then use this to replace the unknown countries in the country column.

