[英]Concatenating a row in a pandas Dataframe
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: 我正在尝试从另一个数据帧向数据帧添加一行(准确地说是2个数据帧),但是出现以下错误:
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? 我是否需要将data['first occurrence of \\'AB\\''],data['similarity to \\'AB\\'']
转换为另一个数据帧? 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
: 编辑:我试过y = pd.concat([y,pd.Series(data['Class'])])
但将数据追加为新列,例如y
:
You need to first convert to dataframe : 您需要先转换为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 编辑:添加ignore_index = True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.