1

I have the following (sample) dataframe:

      Age   height  weight  haircolor
joe    35    5.5      145     brown
mary   26    5.25     110     blonde
pete   44    6.02     185     red
....

There are no duplicate values in the index.

I am in the unenviable position of having to append to this dataframe using elements from a number of other dataframes. So I'm appending as follows:

names_df = names_df.append({'Age': someage, 
                            'height': someheight, 
                            'weight':someweight, 
                            'haircolor': somehaircolor'}, 
                             ignore_index=True)

My question is using this method how do I set the new index value in names_df equal to the person's name?

The only thing I can think of is to reset the df index before I append and then re-set it afterward. Ugly. Has to be a better way.

Thanks in advance.

2 Answers 2

3

I am not sure in what format are you getting the data that you are appending to the original df but one way is as follows:

df.loc['new_name', :] = ['someage', 'someheight', 'someweight', 'somehaircolor']


            Age     height      weight      haircolor
joe         35      5.5         145         brown
mary        26      5.25        110         blonde
pete        44      6.02        185         red
new_name    someage someheight  someweight  somehaircolor

Time Testing:

%timeit df.loc['new_name', :] = ['someage', 'someheight', 'someweight', 'somehaircolor']

1000 loops, best of 3: 408 µs per loop

%timeit df.append(pd.DataFrame({'Age': 'someage', 'height': 'someheight','weight':'someweight','haircolor': 'somehaircolor'}, index=['some_person']))

100 loops, best of 3: 2.59 ms per loop
Sign up to request clarification or add additional context in comments.

3 Comments

I was unaware it could be done this way. I didn't realize you could make a new index entry that way using df.loc. Thanks.
Thanks, not only that it can be done, its a lot more efficient than creating a df for every row and using append. See the edit
Very ugly project with SQL data from all over the place that must be consolidated, evaluated, changed, various elements need to be added and deleted, and results need to be written back to various tables in various databases.
2

Here's another way using append. Instead of passing a dictionary, pass a dataframe (created with dictionary) while specifying index:

names_df = names_df.append(pd.DataFrame({'Age': 'someage',
                            'height': 'someheight',
                            'weight':'someweight',
                            'haircolor': 'somehaircolor'}, index=['some_person']))

2 Comments

Great solution. Thank you.
@Windstorm1981 - Glad it works! Adding on to A-Za-z's comment below, I would not recommend building a dataframe for each entry. I would build up your data dictionary and index separately and create the dataframe once to append.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.