我正在使用StackOverflow的调查数据来分析可以在here中找到的数据。
现在,在删除NaN并将原始数据框中的索引重置为
之后,我有了一个数据框 clear_df index DevType ConvertedComp
0 2 Designer;Developer, back-end;Developer, front-... 8820.0
1 3 Developer, full-stack 61000.0
2 5 Data or business analyst;Data scientist or mac... 366420.0
3 8 Database administrator;Developer, back-end;Dev... 95179.0
4 9 Data or business analyst;Data scientist or mac... 13293.0
因此,我使用了split函数来形成一个新的数据帧,并以'DevType'作为 temp12 。
temp12可以在下面看到。
level_0 level_1 0
0 0 0 Designer
1 0 1 Developer, back-end
2 0 2 Developer, front-end
3 0 3 Developer, full-stack
4 1 0 Developer, full-stack
5 2 0 Data or business analyst
现在,我想使用temp12的 level_0 列作为 clean_df的索引,将'ConvertedComp'从 clean_df 合并到 temp12 。
预期产量
level_0 level_1 0 ConvertedComp
0 0 0 Designer 8820.0
1 0 1 Developer, back-end 8820.0
2 0 2 Developer, front-end 8820.0
3 0 3 Developer, full-stack 8820.0
4 1 0 Developer, full-stack 61000.0
5 2 0 Data or business analyst 366420.0
但是我得到一个错误
TypeError:类型为'NoneType'的对象没有len()
您可以通过运行以下代码从此处here下载数据集来复制错误:
df_2019 = pd.read_csv("dataset/developer_survey_2019/survey_results_public.csv")
def split_column_value(ori_df, column_name, separator=';'):
'''
INPUT - ori_df - pandas dataframe - original dataframe
column_name - string - the name of the column you would like to splite the value
separator - string - The is a delimiter. The string splits at this specified separator. If is not provided then ; is the separator.
OUTPUT -
df - pandas dataframe - all value for the column of original dataframe
'''
ori_df = ori_df.dropna(subset=['DevType','ConvertedComp'])
df = pd.DataFrame(ori_df[column_name].str.split(separator).tolist()).stack()
return df
# splite the DevType colume value
temp1 = split_column_value(df_2019, 'DevType')
temp12 = pd.DataFrame(temp1).reset_index()
temp12.head(20)
clean_df=df_2019.dropna(subset=['DevType','ConvertedComp']).reset_index()[['DevType','ConvertedComp']]
clean_df
# LINE WHICH THROWS THE ERROR
merge_df = temp12.merge(clean_df, right_index=True, right_on='ConvertedComp')