python,pandas并将多个csv导入数据帧

时间:2015-09-09 19:32:30

标签: python csv pandas import dataframe

我的代码从目录中获取多个csv文件,并将所有数据放入我创建的数据框中并称为“df”。每个CSV都是相同的格式,但可以有不同的长度,所以这就是我想要做的事情:

我希望在我的df(DataFrame)中有一个列,它记录每个csv中的第二个到最后一个数据,我在它移动到下一个csv之前。我修改了下面的输出,给你一个我的意思的例子。我们假设我将此列称为BeforeLast。当你看到0值时,这意味着它不是我提取的csv中的倒数第二个数据,如果你看到1值,则意味着它是我所提取的csv中倒数第二个数据。

我怎么能这样做,因为Python正在调用每个被调用的csv?

import pandas as pd
import glob
import os


path =r'X:\PublicFiles\TradingData\CSV\RealMarkets\Weekly\Futures\Contracts\Corn C'
allFiles = glob.glob(path + "/*.csv")  ##'*' means any file name can be grabbed
df = pd.DataFrame()
list_ = []

for file_ in allFiles:
    names = ['Date', 'Open', 'High', 'Low', 'Close', 'Vol', 'OI']
    df = pd.read_csv(file_, index_col = None, names = names)
    list_.append(df)
frame = pd.concat(list_)

以下是我当前dataFrame(df)的示例

    Date       Open    High     Low   Close   Vol  OI
0   20141212  427.00  427.00  427.00  427.00    0   0
1   20141219  429.00  429.00  424.00  424.00    0   0
2   20141226  424.00  425.00  423.00  425.00    0   0
3   20150102  422.75  422.75  417.50  417.50    0   0

这就是我想要的

    Date       Open    High     Low   Close   Vol  OI  BeforeLast
0   20141212  427.00  427.00  427.00  427.00    0   0  0
1   20141219  429.00  429.00  424.00  424.00    0   0  0
2   20141226  424.00  425.00  423.00  425.00    0   0  1
3   20150102  422.75  422.75  417.50  417.50    0   0  0 (this is the last piece of data in this csv and now it moves on to the next)
4   20141226  424.00  425.00  423.00  425.00    0   0  0
5   20150102  422.75  422.75  417.50  417.50    0   0  0
6   20141226  424.00  425.00  423.00  425.00    0   0  1
7   20150102  422.75  422.75  417.50  417.50    0   0  0

3 个答案:

答案 0 :(得分:2)

试试这个。您不需要列表。只需附加到原始数据框。

.iloc [-2,-1]是倒数第二行,最后一列

我添加了一个索引重置,因为在我的测试中我遇到了重复的索引号。

import pandas as pd
import glob
import os


path =r'X:\PublicFiles\TradingData\CSV\RealMarkets\Weekly\Futures\Contracts\Corn C'
allFiles = glob.glob(path + "/*.csv")  ##'*' means any file name can be grabbed
df = pd.DataFrame()

for file_ in allFiles:
    names = ['Date', 'Open', 'High', 'Low', 'Close', 'Vol', 'OI']
    df_temp = pd.read_csv(file_, index_col = None, names = names)
    df_temp['beforelast'] = 0
    df_temp.iloc[-2,-1] = 1
    df = df.append(df_temp)

df = df.reset_index(drop=True)

答案 1 :(得分:0)

df = pd.DataFrame({'a': np.zeros(5)})
df[-2:-1] = 1
print df

   a
0  0
1  0
2  0
3  1
4  0

您可以在创建每个数据框时使用此功能吗?

代码中的示例:

for file_ in allFiles:
    names = ['Date', 'Open', 'High', 'Low', 'Close', 'Vol', 'OI']
    df = pd.read_csv(file_, index_col = None, names = names)
    before = np.zeros(len(df))
    before[-2] = 1
    df['before'] = before
    list_.append(df)
frame = pd.concat(list_)

答案 2 :(得分:0)

只需创建一个列表,以便在构建数据框时跟踪最后一列:

import pandas as pd

df = pd.DataFrame()
newcol = []

for i in range(10):
    # Load 10 files and get shape
    # length = df.shape[0]
    length = 10
    c = [0 for i in range(length)]
    c[-2] = 1
    newcol += c

df['BeforeLast'] = newcol

print df