我有两个CSV格式的数据集,我想在一个pandas数据框中循环并显示它们的结果。现在我在df中的结果只显示B数据集但如果我尝试打印&;data&;它显示了我下面的两个CSV数据集。
测试CSV数据集为:
股票对于读取的每个ticker
行,您将dates
和symbol
清空,它们需要在您的循环之外。如果你重构了一个比特,你应该得到你想要的输出:
import csv
import pandas as pd
with open('test_tickers.csv', newline='') as f_tickers:
csv_tickers = csv.reader(f_tickers)
dates = []
symbols = []
for symbol in csv_tickers:
data = pd.read_csv(f'{symbol[0]}.csv')
i = 1
j = len(data)
in_trade = 0
while i < j:
long = data["fastMA"][i] > data["slowMA"][i]
close = data["fastMA"][i] < data["slowMA"][i]
if long and in_trade == 0:
in_trade = 1
symbols.append(data["Ticker"][i])
dates.append(data["Date"][i])
elif long and in_trade == 1:
in_trade = 1
elif close and in_trade == 1:
in_trade = 0
i += 1
d = {'Date' : dates, 'Ticker' : symbols}
df = pd.DataFrame(d, columns=('Date','Ticker'))
print(df)
给:
Date Ticker
0 05/01/2021 A
1 05/01/2021 B
(假设test_tickers.csv
包含两行包含A
和B
,A.csv
和B.csv
包含您的问题中的数据)