Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将未知长度的数据帧列表与pandas中的另一个数据帧连接起来?_Python_Python 3.x_Pandas - Fatal编程技术网

Python 如何将未知长度的数据帧列表与pandas中的另一个数据帧连接起来?

Python 如何将未知长度的数据帧列表与pandas中的另一个数据帧连接起来?,python,python-3.x,pandas,Python,Python 3.x,Pandas,我正在尝试使用pandas concatenate将一个数据帧列表与另一个数据帧连接起来,以用于张量流的训练函数。该列表包含未知数量的数据帧 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

我正在尝试使用pandas concatenate将一个数据帧列表与另一个数据帧连接起来,以用于张量流的训练函数。该列表包含未知数量的数据帧

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(已弃用)对象是有效的


在连接到
res\u train\u y
之前,我需要分离列表
res\u train\u x
,,正如错误消息所提到的,列表必须是
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)

修复示例的总体代码:

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\u train\u x
res\u train\u y
都是数据帧的列表,则需要连接这些列表,然后连接数据帧,如下所示:

all_dfs = res_train_x + res_train_y
result = pd.concat(all_dfs, axis=0)

正如错误消息所提到的,列表必须是
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)

修复示例的总体代码:

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\u train\u x
res\u train\u y
都是数据帧的列表,则需要连接这些列表,然后连接数据帧,如下所示:

all_dfs = res_train_x + res_train_y
result = pd.concat(all_dfs, axis=0)

如果我正确理解了您的问题,您希望将列表
res\u train\u x
中的每个数据帧连接到数据帧
aa

您可以将其放入循环中,如下所示:

for i in range(len(res_train_x)):
    new_df = pd.concat([res_train_x[i], res_train_y],axis=0)

如果我正确理解了您的问题,您希望将列表
res\u train\u x
中的每个数据帧连接到数据帧
aa

您可以将其放入循环中,如下所示:

for i in range(len(res_train_x)):
    new_df = pd.concat([res_train_x[i], res_train_y],axis=0)

谢谢你的回复!我对我的问题做了一些修改。你能再检查一下吗?在连接它之前,我需要分离
res\u train\u x
。@Black\u Pulse也是一个pd.dataframe或pd.Series?这两个
res\u train\u x
res\u train\u y
都包含pd.dataframes。我想知道我是否可以传递用于连接它们的数据帧,因为我有另一个函数来处理
结果
,如果它是pd.Series或pd.dataframe。感谢您的回复!我对我的问题做了一些修改。你能再检查一下吗?在连接它之前,我需要分离
res\u train\u x
。@Black\u Pulse也是一个pd.dataframe或pd.Series?这两个
res\u train\u x
res\u train\u y
都包含pd.dataframes。我想知道我是否可以传递数据帧来连接它们,因为我有另一个函数来处理
结果
,如果它是pd.Series或pd.dataframe。谢谢!我有一个问题,我是否只需要通过系列,或者如果数据帧本身足以使用串联?我只测试了数据帧,它的工作原理与预期一样。谢谢!我有一个问题,我是否只需要通过序列,或者如果这样的数据帧足以使用concatenate?我只使用数据帧对其进行了测试,它可以按预期工作。