遍历csv列以创建多个python数据框 [英] iterate through csv columns to create multiple python dataframe
问题描述
我正在尝试使用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数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!