大熊猫的Concat桌子

时间:2018-08-13 20:39:13

标签: python pandas

我试图使用熊猫在Excel工作表上执行一些操作,并使用以下代码连接表。

我编写了以下函数来连接字符串并将其保存在数据框中。

def row_split(row, product, measure, price): 


        out_dict = dict.fromkeys(row.index)

        for i in out_dict.keys():
            out_dict[i] = []

        cols = [x for x in out_dict.keys() if x not in [product, measure, price]]

        temp_list = []
        for i, j in zip(row[measure],row[price]):
            try:
                temp_list.append([row['Products'] + ' ' + i, i, int(j)])
            except ValueError:
                temp_list.append([row['Products'], np.nan, np.nan])

        for i in cols:
            for x in row['Measure']:
                out_dict[i].append(row[i])


        for x in temp_list:
            for i, j in zip([product, measure, price], x):
                out_dict[i].append(j)

        return pd.DataFrame(out_dict)

    split_tables = []
    for index, row in (tab1).iterrows():
        split_tables.append(row_split(row, 'Products', 'Measure', 'Price'))

    temp = pd.concat(split_tables[1], split_tables[2])

    final_df = reduce(lambda x, y: pd.concat([x, y]), split_tables)

但是它返回错误-

File "<ipython-input-8-b1c137cbfc9c>", line 48, in <module>
    temp = pd.concat(split_tables[1], split_tables[2])

  File "/anaconda3/lib/python3.6/site-packages/pandas/core/reshape/concat.py", line 225, in concat
    copy=copy, sort=sort)

  File "/anaconda3/lib/python3.6/site-packages/pandas/core/reshape/concat.py", line 241, in __init__
    '"{name}"'.format(name=type(objs).__name__))

TypeError: first argument must be an iterable of pandas objects, you passed an object of type "DataFrame"

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

您应该传递一个可迭代的熊猫对象。不要分开做。

尝试:

temp = pd.concat(split_tables)

或者,如果您真的只想要前两个

temp = pd.concat(split_tables[:2])