my dataframe looks like this:
ID ADDRESS PRICE LOCATION
1 NEW YORK, BROOKLYN, 1 500 NEW YORK
2 LONDON, LONDON, 2 400 " "
.
.
.
I would like to fill the " " with the value from address. I tried something like this:
i = 2008
for addrs, loc in zip(addr[2008:].ADDRSS, addr[2008:].LOCATION):
if addrs.find('NEW YORK') != -1:
addr[i].LOCATION = 'NEW YORK'
if addrs.find('LONDON') != -1:
addr[i].LOCATION = 'LONDON'
if addrs.find('PRAGUE') != -1:
addr[i].LOCATION = 'PRAGUE'
i = i 1
.
.
.
The location didnt fill in from certain row, so thats why there is the addr[2008:]. The locations dont change so I can have them written like that. This code returns KeyError. I dont really know, what is wrong with this, can anybody help?
CodePudding user response:
You can do something like this:
df.loc[df['LOCATION'].eq(' '), 'LOCATION'] = df['ADDRESS']
CodePudding user response:
As I understand it, if there is a " "sign in the LOCATIONS column, it will be filled with the country information in the ADDRESS column.
The following code will be useful for this.
addr['LOCATION'].apply(lambda x: x if x != " " else addr['ADDRESS'].split(sep=',')[0])
I hope it works for you.