Skip to content Skip to sidebar Skip to footer

Python: Getting Typeerror: Expected String Or Bytes-like Object While Calling A Function

I have a text file which was converted to dataframe using below command: df = pd.read_csv('C:\\Users\\Sriram\\Desktop\\New folder (4)\\aclImdb\\test\\result.txt', sep = '\t',

Solution 1:

going by your expected output mentioned: poor - negative excuse - negative I will suggest: trainsets = df.apply(lambda row: ([(kw, row.polarity) for kw in find_features(row.reviews)]), axis=1)

adding a sample snippet for ref:

import pandas as pd
from StringIO import StringIO

print'pandas-version: ', pd.__version__
data_str = """
col1,col2
'leoperd lion tiger','non-veg'
'buffalo antelope elephant','veg'
'dog cat crow','all'
"""
data_str = StringIO(data_str)
# a dataframe with 2 columns
df = pd.read_csv(data_str)

# a dummy function taking a col1 value from each row# and splits it into multiple values & returns a listdefmy_fn(row_val):
    return row_val.split(' ')

# calling row-wise apply vetor operation on dataframe
train_set = df.apply(lambda row: ([(kw, row.col2) for kw in my_fn(row.col1)]), axis=1)
print train_set

output:

pandas-version:  0.15.20    [('leoperd, 'non-veg'), (lion, 'non-veg'), (ti...
1    [('buffalo, 'veg'), (antelope, 'veg'), (elepha...
2        [('dog, 'all'), (cat, 'all'), (crow', 'all')]
dtype: object

@SriramChandramouli, hope I understood your requirement correctly.

Post a Comment for "Python: Getting Typeerror: Expected String Or Bytes-like Object While Calling A Function"