'Pandas, how to add a column iterating the dataframe?

I've a dataset like this:

d = {'state': ['United States', 'IT', 'Spain', 'JP', 'FR'], 'continent': ['North America', 'Europe', 'Europe', 'Asia', 'Europe']}
df = pd.DataFrame(data=d)

with two columns, df['state'] and df['continent']:

United States       North America       
IT                  Europe
Spain               Europe
JP                  Asia
FR                  Europe

I want to create a new column that is composed in this way:

for state in df['state']:
    if(len(state) <= 2):
        # df['newCol'] = df['continent'] + ' - ' + df['state']

So that the result would be:

United States
Europe - IT
Spain
Asia - JP
Europe - FR

But I have some problems with the iteration of the dataset...



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source