1

I hope this has not been posted yet, I have not found anything that helped me. So i have this data frame df

              Id Numero                 Voie  CodePostal          Commune  \
1  940010005V-59     59          Rue d'Ablon       94480  Ablon-sur-Seine   
2  940010005V-61     61          Rue d'Ablon       94480  Ablon-sur-Seine   
3  940010005V-65     65          Rue d'Ablon       94480  Ablon-sur-Seine   

  Source   Latitude  Longitude  \
1    C+O  48.721350   2.414291   
2    C+O  48.722434   2.413538   
3    OSM  48.721141   2.415030   
                                           Adresse AdresseGPS  LatitudeGPS  \
1          59 Rue d'Ablon, Ablon-sur-Seine, France                     0.0   
2          61 Rue d'Ablon, Ablon-sur-Seine, France                     0.0   
3          65 Rue d'Ablon, Ablon-sur-Seine, France                     0.0   

   LongitudeGPS  
1           0.0  
2           0.0  
3           0.0  

I imported it from a csv and added the last three columns using

df = df.assign(AdresseGPS="",LatitudeGPS = 0.,LongitudeGPS = 0.)

What i want to do is modify these last three columns using a function

def funcRow(dataIn):
    dataOut = dataIn
    dataOut['AdresseGPS'] = 't'
    dataOut['LatitudeGPS'] = 1
    return(dataOut)

However when I do

df.ix[1,] = funcRow(df.ix[1,])

I get the following error : IndexError: tuple index out of range

I printed both

df.ix[1,] & funcRow(df.ix[1,])

I get the following:

print df.ix[1,]
     Id                                        940010005V-59
     Numero                                               59
     Voie                                        Rue d'Ablon
     CodePostal                                        94480
     Commune                                 Ablon-sur-Seine
     Source                                              C+O
     Latitude                                        48.7214
     Longitude                                       2.41429
     Adresse         59 Rue d'Ablon, Ablon-sur-Seine, France
     AdresseGPS                                             
     LatitudeGPS                                           0
     LongitudeGPS                                          0
     Name: 1, dtype: object

print funcRow
    Id                                        940010005V-59
    Numero                                               59
    Voie                                        Rue d'Ablon
    CodePostal                                        94480
    Commune                                 Ablon-sur-Seine
    Source                                              C+O
    Latitude                                        48.7214
    Longitude                                       2.41429
    Adresse         59 Rue d'Ablon, Ablon-sur-Seine, France
    AdresseGPS                                            t
    LatitudeGPS                                           1
    LongitudeGPS                                          0
    Name: 1, dtype: object

I am quite new to using data frames with Python so I provided lots of details, not sure if everything is relevant. I have tried this using others functions such as loc or iloc instead of ix but still get the same error. Any advice would be very welcome :)

3 Answers 3

1

I think the "safest" way to solve this is with .loc[] instead of .ix[].

Try this:

def funcRow(dataIn):
    dataOut = dataIn
    dataOut['AdresseGPS'] = 't'
    dataOut['LatitudeGPS'] = 1
    return(dataOut)

df.loc[1,:] = funcRow(df.loc[1,:])

(In case you're not used to .loc[]: the first argument is the row selection, the second argument is the column selection, and giving ":" means you choose all).

When I run the code above I get a warning message, but it does return the updated dataframe if I print df.

(Bonus: This blog post is an excellent reference when learning loc, iloc and ix: http://www.shanelynn.ie/select-pandas-dataframe-rows-and-columns-using-iloc-loc-and-ix/)

Sign up to request clarification or add additional context in comments.

2 Comments

I get the warning as well but it works ! Thanks a lot for both the solution and the blog post ! It is going to be useful !
I'm glad it works! Please click the check-option next to my reply to mark your question as answered :)
0

According to the Documentation,

.ix[] supports mixed integer and label based access. It is primarily label based, but will fall back to integer positional access unless the corresponding axis is of integer type.

I think you want to access last three columns of a whole dataframe values.

If it is you can try,

df.ix[:] = funcRow(df.ix[:])    #for whole rows

or

df.ix[start:end]=funcRow(df.ix[start:end])   #for specific rows

or if you want to access only particular row then you can use this,

df.ix[n] = funcRow(df.ix[n])

I hope it might be help you to solve your problem.

Comments

0

This should work:

df.ix[1] = funcRow(df.ix[1,])

I probably need to take a look at the source code to see why the following doesn't work:

df.ix[1,] = funcRow(df.ix[1,])

1 Comment

Don use ix - check docs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.