How to add new columns into dataframe?

i have a data frame. one of the columns is called ‘Location’
the value of the column contains either three or four comma delimited fields, e.g. ‘LC-39A, Kennedy Space Center, Florida, USA’
I am trying to create 4 new fields in the data frame, each populated with one of these values:

row = 0
items = clean_launches_df[‘Location’].str.split(’,’)
for item in items:
clean_launches_df.loc[row][“pad”] = item[0]
clean_launches_df.loc[row][“site”] = item[1]
clean_launches_df.loc[row][“state”] = item[2]
if len(item) ==3:
clean_launches_df.loc[row][“country”] = item[2]
else:
clean_launches_df.loc[row][“country”] = item[3]
row += 1

clean_launches_df

the problem is that the dataframe that is displayed does not contain the 4 new columns (pad, site, state, country)
What am i leaving out?

also is there a way to do this without resorting to a loop?

df['new column'] = something

i cannot do it as simply as that because i have to parse one of the fields. and the field can parse into 2, 3 or 4 subfields. i have to handle each case individually. In fact, i have added error handling to determine if i am getting an error along the way - i am not. i also displayed the number of records processed - they are all processed.
I cannot figure out why i am not adding the new columns to my DF
this is the new code:

row = 0
items = clean_launches_df[‘Location’].str.split(’,’)
for item in items:
try:
if len(item) ==4:
clean_launches_df.loc[row][‘pad’] = item[0]
clean_launches_df.loc[row][‘site’] = item[1]
clean_launches_df.loc[row][‘state’] = item[2]
clean_launches_df.loc[row][‘country’] = item[3]
elif len(item) ==3:
clean_launches_df.loc[row][‘pad’] = item[0]
clean_launches_df.loc[row][‘site’] = item[1]
clean_launches_df.loc[row][‘state’] = item[2]
clean_launches_df.loc[row][‘country’] = item[2]
elif len(item) ==2:
clean_launches_df.loc[row][‘pad’] = ‘undefined’
clean_launches_df.loc[row][‘site’] = item[0]
clean_launches_df.loc[row][‘state’] = item[1]
clean_launches_df.loc[row][‘country’] = item[1]
else:
print (“Did not process {} row because len = {}”.format(row, len(item)))
row += 1
except:
print (“Error in row {} items = {}”.format(row, items))

print (“Processed {} rows”.format(row))

You can’t have rows with different number of columns. You can though have columns that are “empty” (Not Available)

i am creating the 4 new columns for each condition. in fact, for one of the columns when there are only 2 ‘split’ values, i am entering ‘undefined’. so, each row should have the same number of columns.
in addition, i would think that i would get an error if i did that - and i am not getting any errors

Ok, so I would try this:

pads = [item[0] if len(item) > 2 else None for item in items]
sites = [item[1] if len(item) > 2 else item[0] for item in items]
states = [item[2] if len(item) > 2 else item[1] for item in items]
countries = [item[-1] for item in items]

df['pad'] = pads
df['site'] = sites
df['state'] = states
df['country'] = countries

No idea if it will work though, I wrote from memory :stuck_out_tongue:

you are a genius!
thanks it works.

i was not familiar with this construct: pads = [item[0] if len(item) > 2 else None for item in items]

Just a list comprehension with a ternary operator inside :stuck_out_tongue: