简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM