将str转换为整数的有效方法

时间:2017-07-30 13:18:57

标签: python pandas dataframe

我有一个如下所示的数据框:

Index([u'Date', u'Time', u'Hi Temperature', u'Low Temperature', u'HIT?', u'LOT?', u'Date+Time'], dtype='object')

我创建了列'HIT?','很多?'和'日期+时间'

我正在尝试创建一个'Date+Time'列表,其中验证了某个条件,例如:

data2['HIT?'] is 'TRUE' OR data2['LOT?'] = 'TRUE'.

我运行脚本

Hi_Low = []
for i in data2['Date+Time']:
    if (data2[data2['Date+Time']==map(str,i)['HIT?']] == True) or (data2[data2['Date+Time']==map(str,i)]['LOT?']== True):
        Hi_Low.append(i)
    else:
        pass

但得到了错误:

  

TypeError: list indices must be integers, not str

我认为函数map会转换索引,但是没有这样做,任何人都可以帮我解决这个问题?或任何其他方式来解决这个问题?

1 个答案:

答案 0 :(得分:1)

我认为您需要使用boolean indexing loc来仅过滤列Date+Time

df = pd.DataFrame({'HIT?':[True, False, True, False],
                   'LOT?':[True, True, False, False],
                   'Date+Time':pd.date_range('2017-01-01 13:49:02', periods=4),
                   'Hi Temperature':[10,40,50,60],
                   'Low Temperature':[2,5,7,8]})
print (df)
  Date+Time   HIT?  Hi Temperature   LOT?  Low Temperature
0 2017-01-01   True              10   True                2
1 2017-01-02  False              40   True                5
2 2017-01-03   True              50  False                7
3 2017-01-04  False              60  False                8

Hi_Low = df.loc[df['HIT?'] | df['LOT?'], 'Date+Time'].tolist() 
print (Hi_Low)
[Timestamp('2017-01-01 13:49:02'), 
 Timestamp('2017-01-02 13:49:02'), 
 Timestamp('2017-01-03 13:49:02')]

Hi_Low = df.loc[df['HIT?'] | df['LOT?'], 'Date+Time'].astype(str).tolist() 
print (Hi_Low)
['2017-01-01 13:49:02', '2017-01-02 13:49:02', '2017-01-03 13:49:02']

与:

相同
Hi_Low = df.loc[(df['HIT?'] == True) | 
                (df['LOT?'] == True), 'Date+Time'].astype(str).tolist() 
print (Hi_Low)
['2017-01-01 13:49:02', '2017-01-02 13:49:02', '2017-01-03 13:49:02']

也可以使用|or),&and)或~not)进行链接的条件:

Hi_Low = df.loc[(df['Hi Temperature'] > 45) | 
                (df['Low Temperature'] < 5), 'Date+Time'].astype(str).tolist() 
print (Hi_Low)
['2017-01-01 13:49:02', '2017-01-03 13:49:02', '2017-01-04 13:49:02']