簡體   English   中英

python,pandas並將多個csv導入數據框

[英]python, pandas and importing multiple csv's into a dataframe

我的代碼是從目錄中抓取多個csv文件,並將所有數據放入我創建並稱為“ df”的dataFrame中。 每個CSV都是相同的格式,但是可以有不同的長度,所以這就是我想要做的:

我想在df(DataFrame)中有一列,以記錄我拉入的每個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

嘗試這個。 您不需要列表。 只需追加到原始數據框即可。

.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)
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_)

只需創建一個列表即可在構建數據框時跟蹤最后一列:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM