所以,这是我的数据框
import pandas as pd cols = ['Name','Country','Income'] vals = [['Steve','USA',40000],['Matt','UK',40000],['John','USA',40000],['Martin','France',40000],] x = pd.DataFrame(vals,columns=cols)
我还有另一个清单:
europe = ['UK','France']
如果x.Country位于欧洲,我想创建一个新列“ Continent”
或者您可以isin直接 使用
isin
x['New Column']='Not Europe' x.loc[x.Country.isin(europe),'New Column']='Europe' Out[612]: Name Country Income New Column 0 Steve USA 40000 Not Europe 1 Matt UK 40000 Europe 2 John USA 40000 Not Europe 3 Martin France 40000 Europe