Reputation: 111
I have a data frame that looks like this:
Index([u'Date', u'Time', u'Hi Temperature', u'Low Temperature', u'HIT?',
u'LOT?', u'Date+Time'],
dtype='object')
I created the columns 'HIT?', 'LOT?' and 'Date+Time'
I am trying to create a list of 'Date+Time'
in which a certain condition is verified, e.g.:
data2['HIT?'] is 'TRUE' OR data2['LOT?'] = 'TRUE'.
I run the script
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
but got the error:
TypeError: list indices must be integers, not str
I thought that the function map
would convert the indices, but is not doing it, anybody can give me hand with this?? or any other way to approach this problem?
Upvotes: 1
Views: 57
Reputation: 863791
I think you need boolean indexing
with loc
for filtering only column 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']
It is same as:
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']
Also is possible use conditions with chaining by |
(or
), &
(and
) or ~
(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']
Upvotes: 1