nat
nat

Reputation: 1591

appending columns produces NaN in pandas DataFrame

I need to add columns iteratively to a DataFrame object. This is a simplified version:

>>> x=DataFrame()
>>> for i in 'ps':
...  x = x.append(DataFrame({i:[3,4]}))
... 
>>> x
    p   s
0   3 NaN
1   4 NaN
0 NaN   3
1 NaN   4

What should I do to get:

    p   s
 0  3   3
 1  4   4

?

Upvotes: 3

Views: 2822

Answers (1)

DSM
DSM

Reputation: 353059

Your idea of creating the dict first is probably the best way:

>>> from pandas import *
>>> DataFrame({c: [1,2] for c in 'sp'})
   p  s
0  1  1
1  2  2

(here using dictionary comprehensions, available in Python 2.7). Just for completeness, though, you could -- inefficiently -- use join or concat to get a column-by-column approach to work:

>>> df = DataFrame()
>>> for c in 'sp':
...     df = concat([df, DataFrame({c: [1,2]})], axis=1)
... 
>>> print df
   s  p
0  1  1
1  2  2
>>> 
>>> df = DataFrame()
>>> for c in 'sp':
...     df = df.join(DataFrame({c: [1,2]}), how='outer')
... 
>>> print df
   s  p
0  1  1
1  2  2

[You can see the difference in column order.] But your idea of building the dict and then constructing the DataFrame from the constructed dict is a much better approach.

Upvotes: 2

Related Questions