I’m getting TypeError: cannot concatenate object of type ‘<class ‘str’>’; only Series and DataFrame objs are valid

  append, concatenation, dataframe, json, python

My purpose is to get in each row the id, subject and words in each row and 3 columns for each. for example:

1 id, subject, words
2 id, subject, words

and so on

I’m getting in CSV one long column with id in the first row, subject in the second row and words in the third row for every index. that happens because append can get each one and not all of them together. I tried to concatenate them, but it’s a list. So What should I need to do?

I’m getting TypeError: cannot concatenate object of type ‘<class ‘str’>’; only Series and DataFrame objs are valid

this row is the problem:

data_order=pd.concat(data, keys=['ID', 'subject','words'], axis=1)
class Payload(object):
     def __init__(self, result):
        self.__dict__ = json.loads(result)


data = []
#df = pd.DataFrame()
for row in res:
            result=( ''.join(row[0]))
#             print(result)
#             print("n")
            p = Payload(result)
            data.append(p.ID)
            data.append(p.subject)
            s=p.words.split("|")
            data.append(s)
            #print(s)
            print(p.ID, p.subject,s)
            
            
data_order=pd.concat(data, keys=['ID', 'subject','words'], axis=1)
df = pd.DataFrame(data)
df.to_csv('test.csv', sep='t', encoding='utf-8', index=False) 

Thank you in advance

Source: Python Questions

LEAVE A COMMENT