Reputation: 1316
This seems like it should be so much simpler yet here I am.
I'm trying to add a row to a data frame (2 data frames to be exact) from another data frame, but I get the following error:
TypeError: cannot concatenate object of type "<class 'numpy.float64'>"; only pd.Series, pd.DataFrame, and pd.Panel (deprecated) objs are valid
My code
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']])
Do I have to transform data['first occurrence of \'AB\''],data['similarity to \'AB\'']
into another data frame? This seems horribly inefficient
EDIT: I tried y = pd.concat([y,pd.Series(data['Class'])])
but that appended the data as a new column, example for y
:
Upvotes: 4
Views: 22899
Reputation: 3591
You need to first convert to 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)
EDIT : add ignore_index=True
Upvotes: 2