Concatenate a list of dfs pandas.

huangapple go评论59阅读模式
英文:

Concatenate a list of dfs pandas

问题

你可以使用pandas库中的concat函数来实现这个操作。以下是如何将df_list中的数据框连接成所需的最终数据框的代码:

import pandas as pd

# df_list包含你提供的三个数据框
# 请确保df_list中的数据框按照你想要的顺序排列
# 如果需要,你可以通过重新排列df_list中的数据框来满足你的需求

# 使用concat函数连接数据框
final_df = pd.concat(df_list, axis=1, ignore_index=True)

# 如果你想要特定的列名,可以设置final_df的列名
final_df.columns = range(final_df.shape[1])

# 打印最终的数据框
print(final_df)

这段代码将会连接df_list中的数据框,创建一个具有12列和5行的最终数据框,就像你描述的那样。

英文:

I have a df_list

  [0  1  2  3
0  X  X  0  0
1  X  X  X  X,
   0  1  2  3
0  X  X  X  X
1  0  0  X  X,
   0  1  2  3
0  X  X  X  X]

I would like to concatenate the dfs such that the final df has 12 columns and 5 rows. For example, the first two rows and first 4 cols will have the values of the first df in the df_list and the first two rows and last 8 columns should have the value 0. Like so

    0	1	2	3	4	5	6	7	8	9	10	11
0	X	X	0	0	0	0	0	0	0	0	0	0
1	X	X	X	X	0	0	0	0	0	0	0	0
2	0	0	0	0	X	X	X	X	0	0	0	0
3	0	0	0	0	0	0	X	X	0	0	0	0
4	0	0	0	0	0	0	0	0	X	X	X	X

How can I do this?

答案1

得分: 0

以下是您要翻译的代码部分:

假设lst是输入的列表,您可以更改连续数据帧的索引:

lst2 = []
start_idx = 0
start_col = 0
for d in lst:
    lst2.append(d.set_axis(range(start_idx, start_idx+len(d)))
                 .set_axis(range(start_col, start_col+d.shape[1]), axis=1)
               )
    start_idx += len(d)
    start_col += d.shape[1]

out = pd.concat(lst2).fillna(0)

或者使用重塑:

out = pd.concat(lst, keys=range(len(lst)))
out.index = pd.MultiIndex.from_arrays([out.index.get_level_values(0),
                                       range(len(out))])

out = out.stack().unstack([0, -1], fill_value=0)
out = out.set_axis(range(out.shape[1]), axis=1)

输出:

  0  1  2  3  4  5  6  7  8  9  10 11
0  X  X  0  0  0  0  0  0  0  0  0  0
1  X  X  X  X  0  0  0  0  0  0  0  0
2  0  0  0  0  X  X  X  X  0  0  0  0
3  0  0  0  0  0  0  X  X  0  0  0  0
4  0  0  0  0  0  0  0  0  X  X  X  X

希望这有助于您的工作!

英文:

Assuming lst the input list, you can change the index of the successive DataFrames:

lst2 = []
start_idx = 0
start_col = 0
for d in lst:
    lst2.append(d.set_axis(range(start_idx, start_idx+len(d)))
                 .set_axis(range(start_col, start_col+d.shape[1]), axis=1)
               )
    start_idx += len(d)
    start_col += d.shape[1]

out = pd.concat(lst2).fillna(0)

Or using reshaping:

out = pd.concat(lst, keys=range(len(lst)))
out.index = pd.MultiIndex.from_arrays([out.index.get_level_values(0),
                                       range(len(out))])

out = out.stack().unstack([0, -1], fill_value=0)
out = out.set_axis(range(out.shape[1]), axis=1)

Output:

  0  1  2  3  4  5  6  7  8  9  10 11
0  X  X  0  0  0  0  0  0  0  0  0  0
1  X  X  X  X  0  0  0  0  0  0  0  0
2  0  0  0  0  X  X  X  X  0  0  0  0
3  0  0  0  0  0  0  X  X  0  0  0  0
4  0  0  0  0  0  0  0  0  X  X  X  X

答案2

得分: 0

这是另一种方法:

(pd.DataFrame(
    pd.concat(dict(enumerate(l)))
    .droplevel(1)
    .reset_index()
    .set_index('index', append=True)
    .unstack(level=1, fill_value=0)
    .sort_index(level=-1, axis=1)
    .to_numpy()))

输出:

      0  1  2  3  4  5  6  7  8  9  10 11
    0  X  X  0  0  0  0  0  0  0  0  0  0
    1  X  X  X  X  0  0  0  0  0  0  0  0
    2  0  0  0  0  X  X  X  X  0  0  0  0
    3  0  0  0  0  0  0  X  X  0  0  0  0
    4  0  0  0  0  0  0  0  0  X  X  X  X
英文:

Here is another way:

(pd.DataFrame(
    pd.concat(dict(enumerate(l)))
    .droplevel(1)
    .reset_index()
    .set_index('index',append=True)
    .unstack(level=1,fill_value=0)
    .sort_index(level=-1,axis=1)
    .to_numpy()))

Output:

  0  1  2  3  4  5  6  7  8  9  10 11
0  X  X  0  0  0  0  0  0  0  0  0  0
1  X  X  X  X  0  0  0  0  0  0  0  0
2  0  0  0  0  X  X  X  X  0  0  0  0
3  0  0  0  0  0  0  X  X  0  0  0  0
4  0  0  0  0  0  0  0  0  X  X  X  X

huangapple
  • 本文由 发表于 2023年5月25日 19:20:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76331718.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定