简体   繁体   English

如何使用循环 [Pandas, Python] 创建由现有 df 中的列组成的多个日期框架?

[英]How can I create multiple dateframes consisting of columns from an existing df with a loop [Pandas, Python]?

I tried to create multiple dataframes based on the columns of an existing dataframe.我尝试基于现有数据帧的列创建多个数据帧。 To keep the code simple and scalable, I used a loop.为了保持代码简单和可扩展,我使用了一个循环。 This is what I tried:这是我尝试过的:

import pandas as pd

for index in range(df.shape[1]):
    df_index = df.iloc[:, [0, index]]

The output of the above code is one dataframe consisting of the first and last column of the dataframe.上述代码的输出是一个由数据帧的第一列和最后一列组成的数据帧。 The desired output is multiple dataframes that consist of the first column and the index in a single iteration .所需的输出是多个数据帧,由单次迭代中的第一列和索引组成

The dataset I am using consist of 85 columns.我使用的数据集由 85 列组成。 The desired output would consist of 85 dataframes.所需的输出将包含 85 个数据帧。

your code should look like this你的代码应该是这样的

import pandas as pd

dfs = []

for index in range(df.shape[1]):
    dfs.append(df.iloc[:, [0, index]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM