简体   繁体   中英

How to create a new column in a pandas data frame

I am trying to create a new column called "Continent" by grouping the values in another column called "Regions". The only codes that I managed to do are these:

my_data.loc[(my_data ["Region"] == ("Australia and New Zealand")), "Continent"] = "Australia"

But I am having trouble when there is more than one region for a continent. I have done this:

my_data.loc[((my_data ["Region"] == ("Central and Eastern Europe")) & (my_data["Region"] == ("Western Europe"))), "Continent"] = "Europe"

my_data.loc[((my_data ["Region"] == ("Eastern Asia")) & (my_data["Region"] == ("Southeastern Asia"))), "Continent"] = "Asia" & (my_data["Region"] == ("Southern Asia "))), "Continent"] = "Asia"

it seems like it does not recognize the code because when I execute this, it just appears "NaN" in the column, instead of the name of the continent.

Does anybody know what the problem is?

Here is logic problem, never happens one condition AND another one, because test one column.

So need | for bitwise OR :

my_data.loc[((my_data ["Region"] ==  ("Central and Eastern Europe")) | (my_data["Region"] == ("Western Europe"))), "Continent"] = "Europe"

What working same like test by Series.isin :

my_data.loc[my_data ["Region"].isin(["Central and Eastern Europe", "Western Europe"]), "Continent"] = "Europe"

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