Home > Blockchain >  Keep getting TypeError: sequence item 0: expected str instance, float found when using .join
Keep getting TypeError: sequence item 0: expected str instance, float found when using .join

Time:11-23

I have a data frame of lists of names from different studios within a firm. I am trying to create a new column of names for each row with only the people from other studios. Some kind people helped me to write the code of how to put them together but now it's giving me this error

TypeError                                 Traceback (most recent call last)
<ipython-input-21-0ad6b7d93f69> in <module>
     19     other_employees = deepcopy(studio_dev_emp_year)[row["year"], row["dev_parent"]]
     20     other_employees.remove(row["studio_dev_emp_year"])
---> 21     df.loc[i, "other_studio_emp"] = ",".join(other_employees)
     22 
     23 df.to_csv('studio_level.csv',mode='a',index=False, encoding="utf-8")

TypeError: sequence item 0: expected str instance, float found

After some search, it seems the error is because there are NaN values in the column. I tried to mitigate that by using some codes that I found but it keeps giving me the same message... Below is my code.

import pandas as pd
import csv
from copy import deepcopy

filepath = "C:/Users/Untitled Folder/studio_level_dev.csv"
df = pd.read_csv(filepath,encoding='utf-8')
studio_dev_emp_year = {
    (year, dev_parent): list(
        df.loc[
            (df["year"] == year) & (df["dev_parent"] == dev_parent), "studio_dev_emp_year"
        ].values
    )
    for year in df["year"].unique()
    for dev_parent in df["dev_parent"].unique()
}
studio_dev_emp_year = {key: value for key, value in studio_dev_emp_year.items() if value}

for i, row in df.iterrows():
    other_employees = deepcopy(studio_dev_emp_year)[row["year"], row["dev_parent"]]
    other_employees.remove(row["studio_dev_emp_year"])
    df[i]= df.loc[df[i].notna(), "other_studio_emp"] = ",".join(other_employees)
    
df.to_csv('studio_level.csv',mode='a',index=False, encoding="utf-8")

CodePudding user response:

You might need to convert the other_employees items to strings first:

other_employees = ['' for e in other_employees if np.isnan(e) else str(e)]
df[i] = df.loc[df[i].notna(), "other_studio_emp"] = ",".join(other_employees)
  • Related