0

I have a data frame where 4th column contain ASCII values.

mydict=[('1385145553847005',
  '54',
  'NPAVIN',
  '9175042231172',
  '[89,111,117,114,32,78,80,65,86,32,79,110]',
  '20131123000914',
  'NA',
  'NA',
  '0',
  '0',
  'NA',
  'undefined',
  'a4d05539-cd61-43ee-a870-702e20caeaff',
  '0',
  '0',
  '0'),
('1385145553847006',
  '55',
  'NPAVIN1',
  '9175042231171',
  '[78,80,65,86,32,79,110]',
  '20131123000915',
  'NA',
  'NA',
  '0',
  '0',
  'NA',
  'undefined',
  'a4d05539-cd61-43ee-a870-702e20caeaff',
  '0',
  '0',
  '0')
] 

import pandas as pd
df = pd.DataFrame(mydict)

I have written a function that needs to be applied to the 4th column.

def get_ascii(amyl):
    mys=''
    for item in amyl:
        mys= mys+(chr(int(item)))
    return mys

This does not work. I get value Error:

df.apply(get_ascii(df[4]))

Expected Result: The following 2 values should be added to the last column of the data-frame.

'Your NPAV On'
'NPAV On'
1
  • Tried any debugging? Commented Feb 19, 2015 at 13:03

1 Answer 1

1

There are a number of problems here, your values is a literal string of a list of values:

In [295]:

df[4]
Out[295]:
0    [89,111,117,114,32,78,80,65,86,32,79,110]
1                      [78,80,65,86,32,79,110]
Name: 4, dtype: object

I don't know if this is intended or not but you have to convert this to a list.

Secondly your function is working on the entire Series but it's expecting a single row value at a time, if you want to work a row at a time you have to pass pass param axis=1.

Anyway the following works by evaluating the series that is passed in, accessing the only element value in that series, evaluating as a list and then performing the transliteration:

In [294]:

import ast
def get_ascii(amyl):
    l = ast.literal_eval(amyl.values[0])
    mys=''
    for item in l:
        mys= mys+(chr(item))           
    return mys
df[[4]].apply(get_ascii, axis=1)
Out[294]:
0    Your NPAV On
1         NPAV On
dtype: object
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.