这似乎应该是这么简单但我在这里。
我正在尝试从另一个数据框向数据帧添加一行(确切地说是2个数据帧),但是我收到以下错误:
TypeError: cannot concatenate object of type "<class 'numpy.float64'>"; only pd.Series, pd.DataFrame, and pd.Panel (deprecated) objs are valid
我的代码
for i in range(0,len(k_means_labels_unique)):
X = pd.DataFrame(columns=['first occurrence of \'AB\'','similarity to \'AB\''])
y = pd.DataFrame(columns=['Class'])
for row in result.iterrows():
data=row[1]
if data['cluster ID'] == i:
X = pd.concat([X,data['first occurrence of \'AB\''],data['similarity to \'AB\'']])
y = pd.concat([y,data['Class']])
我是否必须将data['first occurrence of \'AB\''],data['similarity to \'AB\'']
转换为另一个数据框?这看起来非常低效
编辑:我尝试了y = pd.concat([y,pd.Series(data['Class'])])
但是将数据作为新列添加,y
示例:
答案 0 :(得分:1)
您需要先转换为dataframe:
X = pd.concat([X,pd.DataFrame([[data['first occurrence of \'AB\''],data['similarity to \'AB\'']]],columns=['first occurrence of \'AB\'','similarity to \'AB\''])], ignore_index=True)
y = pd.concat([y,pd.DataFrame([data['Class']], columns=['Class'])], ignore_index=True)
编辑:添加ignore_index = True