Python3 / DataFrame:字符串索引必须是整数

时间:2017-09-09 13:37:51

标签: python-3.x pandas dataframe

我有DataFrame的列表:

deviceId  timeInMilliseconds           callState  \
0  e4774cdda0793f86414e8b9140bb6db4       1455233230229  CALL_STATE_OFFHOOK   
1  e4774cdda0793f86414e8b9140bb6db4       1455233232239     CALL_STATE_IDLE   
2  270c1b084f3f146eb5787075158d9c53       1455233316723  CALL_STATE_OFFHOOK   
3  270c1b084f3f146eb5787075158d9c53       1455233324391     CALL_STATE_IDLE   
4  270c1b084f3f146eb5787075158d9c53       1455234721731  CALL_STATE_OFFHOOK   

  number  numberType      time        data day_of_the_week  \
0  609665996           0  00:27:10  2016-02-12               5   
1  609665996           0  00:27:12  2016-02-12               5   
2  126301484           1  00:28:36  2016-02-12               5   
3  126301484           1  00:28:44  2016-02-12               5   
4  126301484           1  00:52:01  2016-02-12               5   

当callState等于CALL_STATE_IDLE时,我希望从这些DataFrame中获取所有数据。

我尝试过这样:

data_all = [x for x in data if (x['callState'] == 'CALL_STATE_IDLE') ]   

但我有'TypeError:字符串索引必须是整数'。

你能帮帮我吗?

2 个答案:

答案 0 :(得分:3)

我想,你需要做

data_all = data[data['callState'] == 'CALL_STATE_IDLE']

data_all

                           deviceId  timeInMilliseconds        callState  \
1  e4774cdda0793f86414e8b9140bb6db4       1455233232239  CALL_STATE_IDLE
3  270c1b084f3f146eb5787075158d9c53       1455233324391  CALL_STATE_IDLE

      number  numberType      time        data  day_of_the_week
1  609665996           0  00:27:12  2016-02-12                5
3  126301484           1  00:28:44  2016-02-12                5

答案 1 :(得分:0)

另外,使用query

In [871]: df.query('callState == "CALL_STATE_IDLE"')
Out[871]:
                           deviceId  timeInMilliseconds        callState  \
1  e4774cdda0793f86414e8b9140bb6db4       1455233232239  CALL_STATE_IDLE
3  270c1b084f3f146eb5787075158d9c53       1455233324391  CALL_STATE_IDLE

      number  numberType      time        data  day_of_the_week
1  609665996           0  00:27:12  2016-02-12                5
3  126301484           1  00:28:44  2016-02-12                5