我有一个.txt文件,其中包含每天BTC的平均价格。以下是btc.txt的内容:
Date,Price
"Jun 06, 2018",7639.970
"Jun 05, 2018",7567.330
"Jun 04, 2018",7618.500
"Jun 03, 2018",7676.170
"Jun 02, 2018",7590.080
"Jun 01, 2018",7521.070
"May 31, 2018",7450.160
...
我有一个日期列表,我想在列表中找到每个日期的平均BTC值,所以我尝试了这段代码:
import pandas as pd
df = pd.read_csv("btc.txt")
dates = calculating_date() #my list of dates
initial_p = []
for item in dates:
if(item != "N/A"):
print (df[df["Date"] == item]["Price"])
initial_p.append(df[df["Date"] == item]["Price"])
else:
initial_p.append(item)
然而,当我运行它时,我明白了:
...
Name: Price, dtype: float64
111 9827.36
Name: Price, dtype: float64
48 8226.79
Name: Price, dtype: float64
16 8470.66
Name: Price, dtype: float64
26 8747.22
Name: Price, dtype: float64
68 7030.98
而不是:
...
9827.36
8226.79
8470.66
8747.22
7030.98
我可以在代码中更改哪些内容以实现上述结果?
答案 0 :(得分:0)
要获得您可以做的价值:
df[df["Date"] == item]["Price"].values[0]