我定义了一个列变量,但是在尝试打印时找不到它-python

时间:2019-11-10 03:28:49

标签: python pandas dataframe

我有以下代码,当我尝试打印b['price']时无法找到该列。但是,它可以打印带有价格列的数据框。因此,我不确定为什么它不起作用。我的代码如下:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import csv
headers = ['ticker', 'size', 'price', 'unix','type','time']
dtypes = {'ticker': 'str', 'size': 'float', 'price': 'float', 'unix': 'float','type': 'str','time': 'str'}
parse_dates = ['time']
btcnow = pd.read_csv('new 113-115.csv', header=None, names=headers, dtype=dtypes, parse_dates=parse_dates)
now3 = pd.DataFrame(btcnow, columns=['size','time','unix','price'])

now4 = now3[['time','price','size']]
df6 = now4.loc[now4["size"] == 4,'size']
df7 = now4.loc[now4["size"] == 4, "time"]
df8 = now4.loc[now4["size"] == 4, "price"]
result1= [df6,df7,df8]

result1 = pd.concat(result1, axis=1, sort=True)

result1.columns = ['size','orig_time','price']
df10=result1.groupby('orig_time').last().reset_index()
df10 = df10[['size','orig_time','price']]

from datetime import datetime, timedelta


time_interval = timedelta(minutes = 5)
df = now3[[ 'time', 'size', 'price']]


# extract time size for merge
df_time_size=df.loc[:, ['time', 'size']]
df_time_size.loc[:, 'time'] = df_time_size.loc[:, 'time'] + time_interval

# inner join dataframe by size&time
df = df_time_size.merge(df[['time', 'size', 'price']], how = 'inner')
df['orig_time'] = df['time'] - time_interval
df = df.groupby('time').last().reset_index()

df1 = df.loc[df["size"] == 4, "price"]
df2 = df.loc[df["size"] == 4, "time"]
df3 = df.loc[df["size"] == 4, "size"]
df4 = df.loc[df["size"] == 4, "orig_time"]
frames = [df3,df1,df2,df4]
result = pd.concat(frames, axis=1, sort=True)
a = pd.merge(result,df10, on="orig_time")
b = a[['size_x' ,'price_x','time','orig_time','price_y']]
b.columns = ['size',' price','time','orig_time','orig_price']
print(b)
b['price']

我收到的错误是关键错误“价格”。 如果有人可以从错误中发现问题,则执行以下操作:

\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2978             if self.columns.nlevels > 1:
   2979                 return self._getitem_multilevel(key)
-> 2980             indexer = self.columns.get_loc(key)
   2981             if is_integer(indexer):
   2982                 indexer = [indexer]

~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2897                 return self._engine.get_loc(key)
   2898             except KeyError:
-> 2899                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2900         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
   2901         if indexer.ndim > 1 or indexer.size > 1:

2 个答案:

答案 0 :(得分:0)

b.columns = ['size',' price','time','orig_time','orig_price']在分配列名称时在此处有一个空格

答案 1 :(得分:0)

我通过执行以下操作使其工作: let formatter = DateFormatter() formatter.timeStyle = .short formatter.locale = Locale(identifier: "en_US_POSIX") if let parsedTime = formatter.date(from: time) { let dateComponents = Calendar.current.dateComponents([.hour, .minute], from: parsedTime) } 我错误地使用了b.rename(columns = {'size_x':'size' ,'price_x':'price','time':'time','orig_time':'orig_time','price_y':'orig_price'}, inplace = True)