Harrison Chong
Harrison Chong

Reputation: 160

How to add new values to dataframe's columns based on specific row without overwrite existing data

I have a batch of identifier and a pair of values that behave in following manner within an iteration. For example,

print(indexIDs[i], (coordinate_x, coordinate_y))

Sample output looks like

enter image description here

I would like to add these data into dataframe, where I can use indexIDs[i] as row and append incoming pair of values with same identifier in the next consecutive columns

I have attempted to perform following code, which didn't work.

spatio_location = pd.DataFrame()
spatio_location.loc[indexIDs[i], column_counter] = (coordinate_x, coordinate_y)

It was an ideal initial to associate indexIDs[i] as row, however I could not progress to take incoming data without overwriting previous dataframe. I am aware it has something to do with the second line which uses "=" sign.

I am aware my second line is keep overwriting previous result over and over again. I am looking for an appropriate way change my second line to insert new incoming data to existing dataframe without overwriting from time to time.

Appreciate your time and effort, thanks.

Upvotes: 0

Views: 223

Answers (1)

giulio
giulio

Reputation: 157

I'm a bit confuesed from the nature of coordinate_x (is it a list or what?) anyway maybe try to use append

you could define an empty df with three columns

df=pd.DataFrame([],columns=['a','b','c'])

after populate it with a loop on your lists

for i in range TOFILL:
    df=df.append({'a':indexIDs[i],'b':coordinate_x[i],'c':coordinate_y[i]},ignore_index=True)

finally set a columns as index

df=df.set_index('a')

hope it helps

Upvotes: 1

Related Questions