data frame

Val Krem valkrem at yahoo.com
Fri Dec 23 16:55:44 EST 2016


Here is the first few lines of the data


s1.csv 
size,w1,h1
512,214,26
123,250,34
234,124,25
334,213,43

and the script

    a=pd.read_csv("s1.csv", skipinitialspace=True).keys()
    print(a)
i see the following

Index(['size', 'w1', 'h1'], dtype='object')



when I wanted to add the two columns; then I get the following message.

a=pd.read_csv("s1.csv", skipinitialspace=True).keys()
a['test']=a['w1'] + a['h1']
print(a)




data/apps/Intel/intelpython35/lib/python3.5/site-packages/pandas/indexes/base.py:1393: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
return getitem(key)
Traceback (most recent call last):
File "tt.py", line 12, in <module>
a['test']=a['w1'] + a['h1']
File "/data/apps/Intel/intelpython35/lib/python3.5/site-packages/pandas/indexes/base.py", line 1393, in __getitem__
return getitem(key)
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices



On Friday, December 23, 2016 3:09 PM, Peter Otten <__peter__ at web.de> wrote:
Val Krem via Python-list wrote:

> Hi all,
>
> #!/usr/bin/env python
> import sys
> import csv
> import numpy as np
> import pandas as  pd
>
> a= pd.read_csv("s1.csv")
> print(a)
>
>      size  w1  h1
> 0  512  214  26
> 1  123  250  34
> 2  234  124  25
> 3  334  213  43
> 4  a45  223  32
> 5  a12  214  26
>
> I wanted to create a new column by adding the two column values
> as follows
>
> a['test'] = a['w1'] + a['h1']
>
> Traceback (most recent call last):
> File
> "/data/apps/Intel/intelpython35/lib/python3.5/site-
packages/pandas/indexes/base.py",
> line 2104, in get_loc return self._engine.get_loc(key) File
> "pandas/index.pyx", line 139, in pandas.index.IndexEngine.get_loc
> (pandas/index.c:4152) File "pandas/index.pyx", line 161, in
> pandas.index.IndexEngine.get_loc (pandas/index.c:4016) File
> "pandas/src/hashtable_class_helper.pxi", line 732, in
> pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13153)
> File "pandas/src/hashtable_class_helper.pxi", line 740, in
> pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13107)
> KeyError: 'w1'
>
> During handling of the above exception, another exception occurred:
>
> Traceback (most recent call last):
> File "tt.py", line 16, in <module>
> a['test']=a['w1'] + a['h1']
>
> File "pandas/src/hashtable_class_helper.pxi", line 740, in
> pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13107)
> KeyError: 'w1'
>
> Can someone help me what the problem is?
>
> Thank you in advance

Have a look at a.keys(). I suspect that the column name has extra space:

>>> pd.read_csv("s1.csv").keys()
Index([u'size', u' w1', u' h1'], dtype='object')

I that's what you see you can fix it by reading the csv with
skipinitialspace=True:

>>> pd.read_csv("s1.csv", skipinitialspace=True).keys()
Index([u'size', u'w1', u'h1'], dtype='object')


-- 
https://mail.python.org/mailman/listinfo/python-list


/data/apps/Intel/intelpython35/lib/python3.5/site-packages/pandas/indexes/base.py:1393: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
return getitem(key)
Traceback (most recent call last):
File "tt.py", line 12, in <module>
a['test']=a['w1'] + a['h1']
File "/data/apps/Intel/intelpython35/lib/python3.5/site-packages/pandas/indexes/base.py", line 1393, in __getitem__
return getitem(key)
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices







On Friday, December 23, 2016 3:09 PM, Peter Otten <__peter__ at web.de> wrote:
Val Krem via Python-list wrote:

> Hi all,
> 
> #!/usr/bin/env python
> import sys
> import csv
> import numpy as np
> import pandas as  pd
> 
> a= pd.read_csv("s1.csv")
> print(a)
> 
>      size   w1   h1
> 0  512  214   26
> 1  123  250   34
> 2  234  124   25
> 3  334  213   43
> 4  a45  223   32
> 5  a12  214   26
> 
> I wanted to create a new column by adding the two column values
> as follows
> 
> a['test'] = a['w1'] + a['h1']
> 
> Traceback (most recent call last):
> File
> "/data/apps/Intel/intelpython35/lib/python3.5/site-
packages/pandas/indexes/base.py",
> line 2104, in get_loc return self._engine.get_loc(key) File
> "pandas/index.pyx", line 139, in pandas.index.IndexEngine.get_loc
> (pandas/index.c:4152) File "pandas/index.pyx", line 161, in
> pandas.index.IndexEngine.get_loc (pandas/index.c:4016) File
> "pandas/src/hashtable_class_helper.pxi", line 732, in
> pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13153)
> File "pandas/src/hashtable_class_helper.pxi", line 740, in
> pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13107)
> KeyError: 'w1'
> 
> During handling of the above exception, another exception occurred:
> 
> Traceback (most recent call last):
> File "tt.py", line 16, in <module>
> a['test']=a['w1'] + a['h1']
> 
> File "pandas/src/hashtable_class_helper.pxi", line 740, in
> pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13107)
> KeyError: 'w1'
> 
> Can someone help me what the problem is?
> 
> Thank you in advance

Have a look at a.keys(). I suspect that the column name has extra space:

>>> pd.read_csv("s1.csv").keys()
Index([u'size', u' w1', u' h1'], dtype='object')

I that's what you see you can fix it by reading the csv with 
skipinitialspace=True:

>>> pd.read_csv("s1.csv", skipinitialspace=True).keys()
Index([u'size', u'w1', u'h1'], dtype='object')


-- 
https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list