迭代通过csv列创建多个python datafram [英] iterate through csv columns to create multiple python datafram

查看:312
本文介绍了迭代通过csv列创建多个python datafram的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用excel csv文件的列创建多个数据框。这是我能够到达

I am trying to create multiple data frames using the columns of a excel csv file. This is where I have been able to get to

import pandas as pd
file = pd.read_csv('file.csv')
df = pd.DataFrame(file)
cols = df.columns
#column names are 'Date', 'Stock 1', 'Stock 2', etc - I have 1000 columns

for i in range(len(cols)):
    df[i] = df[['Date',b(i)]]

所以最终结果是我想要多个数据帧。第一个数据帧使用列1和2(因此日期和库存1),第二个数据帧使用列1和3(因此日期和库存2),第三个数据帧使用列1和3,创建新的数据帧到列1和1000.

So the end result is I want multiple dataframes. The first dataframe is with columns 1 and 2 (so Date and Stock 1), the second dataframe is with columns 1 and 3 (so Date and Stock 2), the third dataframe is with columns 1 and 3, creating new dataframe all the way to Columns 1 and 1000.

我已经尝试了几种方法,并获得索引不可调用或我尝试与usecols,我得到usecols必须是字符串或整数。

I have tried several ways and either get index in not callable or I tried with usecols and I get usecols must be strings or integers.

任何人都可以帮助我。从概念上来说很容易,但我不能得到正确的代码。谢谢。

Can anyone help me with this. Conceptually it is easy but I can not get the code right. Thank you.

推荐答案

这是你要求的:

all_dfs = []
for col in df.columns:
    if col != 'Date':
        df_current = df[['Date', col]]
        all_dfs.append(df_current)

或作为一行:

all_dfs = [df[['Date', col]] for col in df.columns if col != 'Date']

但你可能不想这样做。没有多少点。你真正想做什么?

But you probably don't want to do that. There's not much point. What are you really trying to do?

这篇关于迭代通过csv列创建多个python datafram的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆