Reputation:
I have an hdf file which is printed out like this:
print dt['data1']
=>
column1 column2 column3
row1 3 8 2
row2 4 3 0
row3 12 1 14
row4 -5 5 25
1) How can I search by rows or rather "select" it? That is,
dt.get_row('row3')
=> row3 12 1 14
2) And how can I search by row and column value? That is
dt.get_row('row2').where(column_value=3)
=> column2
Upvotes: 1
Views: 3989
Reputation: 85442
Setting your the pandas DataFrame
to df
:
df = dt['data1']
You can get the row with ix
:
df.ix['row3']
This gives the result as a Series
:
column1 12
column2 1
column3 14
Name: row3, dtype: int64
You can also access the values as NumPy a array:
>>> df.ix['row3'].values
array([12, 1, 14])
or the name of the row:
>>> df.ix['row3'].name
'row3'
and the column in row2
where the value is equal to 3
with:
r2 = df.ix['row2']
r2[r2.eq(3)].index
This gives you an Index
object:
Index(['column2'], dtype='object')
You can access single elements too:
>>> r2[r2.eq(3)].index[0]
'column2'
Upvotes: 1
Reputation: 31
Here is a code snippet that I could get working. Not sure if it is the best way to do it.
>>> import pandas as pd
>>> df = pd.DataFrame({'AAA' : [4,5,6,7], 'BBB' : [10,20,30,40],'CCC' : [100,50,-30,-50]})
>>> df
AAA BBB CCC<br>
0 4 10 100<br>
1 5 20 50<br>
2 6 30 -30<br>
3 7 40 -50<br>
>>> df.iloc[0]
AAA 4
BBB 10
CCC 100
Name: 0, dtype: int64
>>> df.iloc[0].AAA
4
Hope this is what you were looking for.
Upvotes: 0