data frame

Val Krem valkrem at yahoo.com
Fri Dec 23 15:39:59 EST 2016


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



More information about the Python-list mailing list