Skip to content
Advertisement

How to add values to a new column in pandas dataframe?

I want to create a new named column in a Pandas dataframe, insert first value into it, and then add another values to the same column:

Something like:

import pandas

df = pandas.DataFrame()
df['New column'].append('a')
df['New column'].append('b')
df['New column'].append('c')

etc.

How do I do that?

Advertisement

Answer

Dont do it, because it’s slow:

  1. updating an empty frame a-single-row-at-a-time. I have seen this method used WAY too much. It is by far the slowest. It is probably common place (and reasonably fast for some python structures), but a DataFrame does a fair number of checks on indexing, so this will always be very slow to update a row at a time. Much better to create new structures and concat.

Better to create a list of data and create DataFrame by contructor:

vals = ['a','b','c']

df = pandas.DataFrame({'New column':vals})
Advertisement