Home > Back-end >  TypeError: sequence item 1: expected str instance, int found (Python)
TypeError: sequence item 1: expected str instance, int found (Python)

Time:12-02

Seeking for your assistance regarding this issue and I'm trying to resolve it, tried so many syntax but still getting the same error. I got multiple csv files to be converted and I'm pulling the same data, the script works for 1 of my csv file but not on the other. Looking forward to your feedback. Thank you very much.

My code:

import os

import pandas as pd

directory = 'C:/path' ext = ('.csv')

for filename in os.listdir(directory): f = os.path.join(directory, filename)

if f.endswith(ext):

    head_tail = os.path.split(f)
    head_tail1 = 'C:/path'
    k =head_tail[1]
    r=k.split(".")[0]

    p=head_tail1   "/"   r   " - Revised.csv"
    mydata = pd.read_csv(f)

    # to pull columns and values
    new = mydata[["A","Room","C","D"]]
    new = new.rename(columns={'D': 'Qty. of Parts'})
    new['Qty. of Parts'] = 1
    new.to_csv(p ,index=False)

    #to merge columns and values
    merge_columns = ['A', 'Room', 'C']
    merged_col = ''.join(merge_columns).replace('ARoomC', 'F')

    new[merged_col] = new[merge_columns].apply(lambda x: '.'.join(x), axis=1)
    new.drop(merge_columns, axis=1, inplace=True)
    new = new.groupby(merged_col).count().reset_index()
    new.to_csv(p, index=False)

The error I get:

Traceback (most recent call last):
File "C:Path\MyProject.py", line 34, in <module>
new[merged_col] = new[merge_columns].apply(lambda x:    '.'.join(x), axis=1)
File "C:Path\MyProject.py", line 9565, in apply
return op.apply().__finalize__(self, method="apply")
File "C:Path\MyProject.py", line 746, in apply
return self.apply_standard()
File "C:Path\MyProject.py", line 873, in  apply_standard
results, res_index = self.apply_series_generator()
File "C:Path\MyProject.py", line 889, in  apply_series_generator
results[i] = self.f(v)
File "C:Path\MyProject.py", line 34, in <lambda>
new[merged_col] = new[merge_columns].apply(lambda x: '.'.join(x), axis=1)
TypeError: sequence item 1: expected str instance,  int found

CodePudding user response:

It's hard to say what you're trying to achieve without showing a sample of your data. But anyway, to fix the error, you need to cast the values as a string with str when calling pandas.Series.apply :

new[merged_col] = new[merge_columns].apply(lambda x: '.'.join(str(x)), axis=1)

Or, you can also use pandas.Series.astype:

new[merged_col] = new[merge_columns].astype(str).apply(lambda x: '.'.join(x), axis=1)
  • Related