在Python中将数据帧与具有列标题的空数据帧合并
我想把两个数据帧合并在一起;一个是具有列标题的空数据帧,另一个是大小为18 x 600的数据帧 我尝试的是:在Python中将数据帧与具有列标题的空数据帧合并,python,pandas,dataframe,merge,Python,Pandas,Dataframe,Merge,我想把两个数据帧合并在一起;一个是具有列标题的空数据帧,另一个是大小为18 x 600的数据帧 我尝试的是: userQuestionVector1 = pd.read_csv("embedding1_3.csv") userQuestionVector2 = pd.read_csv("embedding2_3.csv") userQuestionVector = pd.concat([userQuestionVector1,userQuestionVector2],axis=1) new_df
userQuestionVector1 = pd.read_csv("embedding1_3.csv")
userQuestionVector2 = pd.read_csv("embedding2_3.csv")
userQuestionVector = pd.concat([userQuestionVector1,userQuestionVector2],axis=1)
new_df = pd.DataFrame(columns=[vector])
df_userQuestionVector = new_df.append(userQuestionVector)
print(df_userQuestionVector)
在这里,vector
是一个包含600个字符串的列表
['word2vec_q1_1', 'word2vec_q1_2', 'word2vec_q1_3', ..., 'word2vec_q1_300', 'word2vec_q2_1', ..., 'word2vec_q2_300']
new_df
的尺寸为0 x 600
userQuestionVector1
和userQuestionVector2
的维度为18 x 300
userQuestionVector
的维度为18 x 600
输出df_userQuestionVector
维度为18 x 1200,即并排合并两个数据帧,留下NaN值的后半部分
value1_1 value1_2 value1_3 ... value1_300 string1 string2 string3 ... string300
0 value2_1 value2_2 value2_3 ... value2_300 NaN NaN NaN ... NaN
1 value3_1 value3_2 value3_3 ... value3_300 NaN NaN NaN ... NaN
2 value4_1 value4_2 value4_3 ... value4_300 NaN NaN NaN ... NaN
. . . . . . . .
. . . . . . . .
预期输出的维度应为18 X 600,即df_userQuestionVector
应合并到new_df
下面
string1 string2 string3 ... string300
0 value1_1 value1_2 value1_3 ... value1_300
1 value2_1 value2_2 value2_3 ... value2_300
2 value3_1 value3_2 value3_3 ... value3_300
. . . . . .
. . . . . .
我还尝试:
frames=[new_df, userQuestionVector]
df_userQuestionVector = pd.concat(frames,axis=0)
但这给了我同样的结果
我应该如何解决这个问题?谢谢。在阅读csv时,将标题设置为“无”,而不是创建新的数据框,将
用户问题向量
数据框列设置为向量
,即将代码更改为
userQuestionVector1 = pd.read_csv("embedding1_3.csv", header= None)
userQuestionVector2 = pd.read_csv("embedding2_3.csv", header = None)
userQuestionVector = pd.concat([userQuestionVector1,userQuestionVector2],axis=1)
userQuestionVector.columns = vector
希望这有帮助。在读取csv时,将标题设置为“无”,而不是创建一个
新的数据框架,将用户问题向量
数据框架列设置为向量
,即将代码更改为
userQuestionVector1 = pd.read_csv("embedding1_3.csv", header= None)
userQuestionVector2 = pd.read_csv("embedding2_3.csv", header = None)
userQuestionVector = pd.concat([userQuestionVector1,userQuestionVector2],axis=1)
userQuestionVector.columns = vector
希望这有帮助。向量中有什么?为什么不将append和2个数据帧一起使用?@gionni vector是一个包含600个字符串的列表。看看我更新的问题。@gionni你提到的是哪两个数据帧?忽略它,抱歉,我误解了这个问题:)向量中有什么?为什么不将append和2个数据帧一起使用?@gionni vector是一个包含600个字符串的列表。看看我更新的问题。@gionni你提到的是哪两个数据帧?忽略它,抱歉,我误解了这个问题:)@K.K很高兴看到它helped@K.K很高兴这有帮助