获取 TypeError:在 Pandas Dataframe 中设置索引时,“list”对象不可调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37481563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting TypeError: 'list' object is not callable when setting index in Pandas Dataframe
提问by PeterL
I have simple Pandas Dataframe:
我有简单的 Pandas 数据框:
data.head(5)
> Date Time Open High Low Close Vol OI
> 0 02/02/1993 16:00 44.23 44.38 44.13 44.34 201300 0
> 1 02/03/1993 16:00 44.41 44.84 44.38 44.82 529400 0
> 2 02/04/1993 16:00 44.97 45.10 44.88 45.00 531500 0
> 3 02/05/1993 16:00 44.97 45.06 44.73 44.97 492100 0
> 4 02/08/1993 16:00 44.97 45.13 44.92 44.98 596100 0
I want to set Column "Date" as index:
我想将列“日期”设置为索引:
data.set_index('Date')
But get error "TypeError: 'list' object is not callable"
但是得到错误“TypeError:'list'对象不可调用”
TypeError Traceback (most recent call last)
<ipython-input-59-a610da45b82c> in <module>()
----> 1 data.set_index('Date')
TypeError: 'list' object is not callable
Currently the index in data is RangeIndex:
目前数据中的索引是 RangeIndex:
data.index
RangeIndex(start=0, stop=5873, step=1)
Any tip why I cannot set the index using Date column?
为什么我不能使用 Date 列设置索引的任何提示?
Thank you.
谢谢你。
回答by jezrael
I think you have to add parameter inplace=True
to set_index
:
我认为您必须将参数添加inplace=True
到set_index
:
data.set_index('Date', inplace=True)
Another solution is:
另一种解决方案是:
data = data.set_index('Date')
Sample:
样本:
import pandas as pd
data = pd.DataFrame({'High': {0: 44.380000000000003, 1: 44.840000000000003, 2: 45.100000000000001, 3: 45.060000000000002, 4: 45.130000000000003}, 'Vol': {0: 201300, 1: 529400, 2: 531500, 3: 492100, 4: 596100}, 'Close': {0: 44.340000000000003, 1: 44.82, 2: 45.0, 3: 44.969999999999999, 4: 44.979999999999997}, 'Date': {0: '02/02/1993', 1: '02/03/1993', 2: '02/04/1993', 3: '02/05/1993', 4: '02/08/1993'}, 'Open': {0: 44.229999999999997, 1: 44.409999999999997, 2: 44.969999999999999, 3: 44.969999999999999, 4: 44.969999999999999}, 'Time': {0: '16:00', 1: '16:00', 2: '16:00', 3: '16:00', 4: '16:00'}, 'Low': {0: 44.130000000000003, 1: 44.380000000000003, 2: 44.880000000000003, 3: 44.729999999999997, 4: 44.920000000000002}, 'OI': {0: 0, 1: 0, 2: 0, 3: 0, 4: 0}})
print (data)
# Close Date High Low OI Open Time Vol
#0 44.34 02/02/1993 44.38 44.13 0 44.23 16:00 201300
#1 44.82 02/03/1993 44.84 44.38 0 44.41 16:00 529400
#2 45.00 02/04/1993 45.10 44.88 0 44.97 16:00 531500
#3 44.97 02/05/1993 45.06 44.73 0 44.97 16:00 492100
#4 44.98 02/08/1993 45.13 44.92 0 44.97 16:00 596100
print (data.columns)
#Index(['Close', 'Date', 'High', 'Low', 'OI', 'Open', 'Time', 'Vol'], dtype='object')
data.set_index('Date', inplace=True)
print (data)
# Close High Low OI Open Time Vol
#Date
#02/02/1993 44.34 44.38 44.13 0 44.23 16:00 201300
#02/03/1993 44.82 44.84 44.38 0 44.41 16:00 529400
#02/04/1993 45.00 45.10 44.88 0 44.97 16:00 531500
#02/05/1993 44.97 45.06 44.73 0 44.97 16:00 492100
#02/08/1993 44.98 45.13 44.92 0 44.97 16:00 596100
If need set index and convert to datetime in read_csv
use (if separator is ,
you can omit it, because by default sep=','
):
如果需要设置索引并转换为read_csv
使用中的日期时间(如果分隔符是,
你可以省略它,因为默认情况下sep=','
):
import pandas as pd
import io
temp=u"""Date;Time;Open;High;Low;Close;Vol;OI
02/02/1993;16:00;44.23;44.38;44.13;44.34;201300;0
02/03/1993;16:00;44.41;44.84;44.38;44.82;529400;0
02/04/1993;16:00;44.97;45.10;44.88;45.00;531500;0
02/05/1993;16:00;44.97;45.06;44.73;44.97;492100;0
02/08/1993;16:00;44.97;45.13;44.92;44.98;596100;0"""
#after testing replace io.StringIO(temp) to filename
data = pd.read_csv(io.StringIO(temp), sep=";", index_col='Date', parse_dates=['Date'])
print (data)
Time Open High Low Close Vol OI
Date
1993-02-02 16:00 44.23 44.38 44.13 44.34 201300 0
1993-02-03 16:00 44.41 44.84 44.38 44.82 529400 0
1993-02-04 16:00 44.97 45.10 44.88 45.00 531500 0
1993-02-05 16:00 44.97 45.06 44.73 44.97 492100 0
1993-02-08 16:00 44.97 45.13 44.92 44.98 596100 0
回答by Aamir M. Khan
I got the same issue of "TypeError: 'list' object is not callable" when using the set_index command. I got the solution by first calling 'reindex()' method and then using set_index. Hope it works for you. Aamir
使用 set_index 命令时,我遇到了同样的问题“TypeError: 'list' object is not callable”。我通过首先调用“reindex()”方法然后使用 set_index 得到了解决方案。希望对你有效。阿米尔