Rich Rich - 4 months ago 67
Python Question

Can't select a row from a pandas dataframe, read from a csv

I have a csv with some data that I'm reading into pandas:

filename = sys.argv[1]

data = pd.read_csv(filename, sep=';', header=None)

xy = data

print str(xy)


Result:

0 1
0 label data
1 x 6,8,10,14,18
2 y 7,9,13,17.5,18
3 z 0,0,1,1,1
4 r 2,13,31,33,34,4324,32413,431,666


However, when I try to select a frame:

xy = data['2']
xy = data['y']
xy = data['label']


It just gives me the same error:

Traceback (most recent call last):
File "Regress[AA]--[01].py", line 10, in <module>
xy = data['label']
File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 1997, in __getitem__
return self._getitem_column(key)
File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 2004, in _getitem_column
return self._get_item_cache(key)
File "/usr/local/lib/python2.7/dist-packages/pandas/core/generic.py", line 1350, in _get_item_cache
values = self._data.get(item)
File "/usr/local/lib/python2.7/dist-packages/pandas/core/internals.py", line 3290, in get
loc = self.items.get_loc(item)
File "/usr/local/lib/python2.7/dist-packages/pandas/indexes/base.py", line 1947, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas/index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas/index.c:4154)
File "pandas/index.pyx", line 161, in pandas.index.IndexEngine.get_loc (pandas/index.c:4084)
KeyError: 'label'


How should I be formatting my selection request?

EDIT: Thanks to @Merlin's help, I got it working:

filename = sys.argv[1]
df = pd.read_csv(filename, sep=';')

for i in range(len(df.label)):
a = str(df['label'][i])
b = str(df['data'][i])
print ("Row: {} - Data: {}".format(a,b))


Gives me:

Row: x - Data: 6,8,10,14,18
Row: y - Data: 7,9,13,17.5,18
Row: z - Data: 0,0,1,1,1
Row: r - Data: 2,13,31,33,34,4324,32413,431,666

Answer

Try this:

filename = sys.argv[1]
df       = pd.read_csv(filename, sep=';')
xy       = df

Do not name your dataframe "data"; One of your column headers is named data!. Then: for i, row in df.iterrows(): a = str(df['label'][i]) b = str(df['data'][i]) print ("Row: {} - Data: {}".format(a,b))

 print df.head()
 print df.info()
 print df["data"].head() 

I dont know what you are expecting

from StringIO import StringIO
import pandas as pd

text = u"""label;data
x;6,8,10,14,18
y;7,9,13,17.5,18
z;0,0,1,1,1
r;2,13,31,33,34,4324,32413,431,666"""

df = pd.read_csv(StringIO(text),sep=';')

df

      label                          data
0     x                      6,8,10,14,18
1     y                    7,9,13,17.5,18
2     z                         0,0,1,1,1
3     r  2,13,31,33,34,4324,32413,431,666

df.head()

  label                              data
0     x                      6,8,10,14,18
1     y                    7,9,13,17.5,18
2     z                         0,0,1,1,1
3     r  2,13,31,33,34,4324,32413,431,666

df.info()

   <class 'pandas.core.frame.DataFrame'>
RangeIndex: 4 entries, 0 to 3
Data columns (total 2 columns):
label    4 non-null object
data     4 non-null object
dtypes: object(2)
memory usage: 136.0+ bytes

df["data"][1]
'7,9,13,17.5,18'

df["label"]
0    x
1    y
2    z
3    r
Name: label, dtype: object

Another edit:

for i, row in df.iterrows():
    a = str(df['label'][i])
    b = str(df['data'][i])
    print ("Row: {} - Data: {}".format(a,b))