简体   繁体   中英

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

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")

You might need to convert the other_employees column values to strings first. Just add .astype('str') to do that:

df[i] = df.loc[df[i].notna(), "other_studio_emp"] = ",".join(other_employees.astype('str'))
#                                                                           ^^^^^^^^^^^^^^ Convert the data to strings before joining.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM