Home > Back-end >  Pandas, how to add a column iterating the dataframe?
Pandas, how to add a column iterating the dataframe?

Time:02-10

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...

CodePudding user response:

Use np.where:

df['new'] = np.where(df['state'].str.len() <= 2, df['continent']   ' - '   df['state'], df['state'])

Output:

>>> df
           state      continent            new
0  United States  North America  United States
1             IT         Europe    Europe - IT
2          Spain         Europe          Spain
3             JP           Asia      Asia - JP
4             FR         Europe    Europe - FR
  • Related