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:
JavaScript
x
9
1
import pandas
2
3
df = pandas.DataFrame()
4
df['New column'].append('a')
5
df['New column'].append('b')
6
df['New column'].append('c')
7
8
etc.
9
How do I do that?
Advertisement
Answer
Dont do it, because it’s slow:
- 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:
JavaScript
1
4
1
vals = ['a','b','c']
2
3
df = pandas.DataFrame({'New column':vals})
4