Inserting Pandas DataFrame column

This is the place for queries that don't fit in any of the other categories.

Inserting Pandas DataFrame column

Postby tnknepp » Mon Aug 26, 2013 6:32 pm

There are a few ways to introduce new rows to pandas data frames, but one of them exhibits odd behavior.

Code: Select all
import pandas as pd
from magic_square import magic as ms

a = pd.DataFrame(ms(5),columns=list('ABCDE'))
b = pd.DataFrame(ms(5)[:,0],columns=['R'])

# Works
a['R'] = b

# Works
a = pd.concat([a,b],axis=1,ignore_index=False/True)

# Works
a = a.join(b)

# Works, though also adds rows
a = a.append(b,ignore_index=False)

# Odd behavior
a.R = b


The last example works, in that a.R is recognized, e.g.
Code: Select all
>>> a.R
    R
0  17
1  23
2   4
3  10
4  11


But, this fails to be noticed as a legitimate column:
Code: Select all
>>> a.columns
Index([A, B, C, D, E], dtype=object)

>>> a
    A   B   C   D   E
0  17  24   1   8  15
1  23   5   7  14  16
2   4   6  13  20  22
3  10  12  19  21   3
4  11  18  25   2   9

>>> type(a.R)
<class 'pandas.core.frame.DataFrame'>

>>> a.dtypes # doesn't show up here
A    int32
B    int32
C    int32
D    int32
E    int32
dtype: object


In the last example, what is a.R really? It seems to have some sort of dual behavior wherein it is trapped between worlds (perhaps a zombie column).
Python: 2.7 via Anaconda
Numpy: 1.7
Pandas: 0.11
OS: Windows 7
IDE: Spyder/IPython
User avatar
tnknepp
 
Posts: 134
Joined: Mon Mar 11, 2013 7:41 pm

Re: Inserting Pandas DataFrame column

Postby stranac » Tue Aug 27, 2013 6:37 pm

I could be wrong, but I think that's not recognized as inserting a column, but setting an attribute on the a object.
The fact that that attribute is being set to a DataFrame makes no difference.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1218
Joined: Thu Feb 07, 2013 3:42 pm

Re: Inserting Pandas DataFrame column

Postby tnknepp » Thu Aug 29, 2013 6:31 pm

Thanks again stranac.

That would explain why .R does not get copied with into b:

Code: Select all
b = a.copy()
>>>b.R
AttributeError   
Python: 2.7 via Anaconda
Numpy: 1.7
Pandas: 0.11
OS: Windows 7
IDE: Spyder/IPython
User avatar
tnknepp
 
Posts: 134
Joined: Mon Mar 11, 2013 7:41 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests