我试图连接一个数据帧列表与另一个数据帧使用熊猫串接用于张量流的训练功能。该列表包含未知数目的数据帧。
import pandas as pd
res_train_x = [a, b, c, d, e....]
# here each variable is a data frame. Ex: a = [], b = [], c = [] and so on
res_train_y = aa
# this is how I need the code to work
result = pd.concat([a, b, c, d, e, ..., aa], axis=0)
# my existing code
result = pd.concat([res_train_x, res_train_y], axis=0)当我运行我现有的代码时,我会得到这个错误。
TypeError:不能连接类型为“”的对象;只有pd.Series、pd.DataFrame和pd.Panel (废弃的) objs是有效的
在与res_train_x连接之前,我需要分离列表res_train_y。
发布于 2019-05-16 08:40:07
正如错误消息所提到的,这些列表至少需要是pd.Series类型的,才能使连接工作。要做到这一点,您可以简单地在列表上应用pd.Series,然后您将能够连接。下面是一个例子
import pandas as pd
# given two lists a and b
a = [1, 2, 3]
b = [4, 5, 6]
# if you try to concatenate them with converting to pd.Series
pd.concat([a, b], axis=0)
# You will get a type error:
# TypeError: cannot concatenate object of type "<type 'list'>"; only pd.Series, pd.DataFrame, and pd.Panel (deprecated) objs are valid
# if you convert to pd.Series before concatenate, it works:
pd.concat([pd.Series(a), pd.Series(b)], axis=0)示例输出如下:
Out[5]:
0 1
1 2
2 3
0 4
1 5
2 6
dtype: int64修复示例的总体代码:
import pandas as pd
res_train_x = [1, 2, 3]
res_train_y = [4, 5, 6]
result = pd.concat([pd.Series(res_train_x), pd.Series(res_train_y)], axis=0)对最新问题的答复:
如果res_train_x和res_train_y都是dataframe列表,则需要将这些列表连接起来,然后将数据连接起来,如下所示:
all_dfs = res_train_x + res_train_y
result = pd.concat(all_dfs, axis=0)发布于 2019-05-16 08:55:29
如果我正确理解您的问题,您希望将列表res_train_x中的每个数据帧连接到数据框架aa。
您可以将其放入循环中,如下所示:
for i in range(len(res_train_x)):
new_df = pd.concat([res_train_x[i], res_train_y],axis=0)https://stackoverflow.com/questions/56164261
复制相似问题