How do I go about selecting a column data in a dataframe for specific row values ​​in python?

As the question says, I have a dataframe that is quite large, but looks like this:

        ID    Count    ValueX    Value 2    Value 3
RowX    1      234.     255.       yes.      yes
RowY    1      123.     135.       543.      342
RowW    1      234.     235.       yes.      yes
RowJ    1      123.     115.       543.      342
RowA    1      234.     285.       yes.      yes
RowR    1      123.     165.       543.      342
RowX    2      234.     255.       yes.      yes
RowY    2      123.     135.       543.      342
RowW    2      234.     235.       yes.      yes
RowJ    2      123.     115.       543.      342
RowA    2      234.     285.       yes.      yes
RowR    2      123.     165.       543.      342
.
.
.
RowX    1233   234.     255.       yes.      yes
RowY    1233   123.     135.       543.      342
RowW    1233   234.     235.       yes.      yes
RowJ    1233   123.     115.       543.      342
RowA    1233   234.     285.       yes.      yes
RowR    1233   123.     165.       543.      342

      

I want to be able to select all the values ​​in a column ValueX

where the row is RowX

for each of the ID numbers 1-1233 and return them to a list.

+3


source to share


3 answers


IIUC:



In [30]: df.loc[df.index.isin(['RowX']) & df['ID'].between(1, 1233), 'ValueX'].tolist()
Out[30]: [255.0, 255.0, 255.0]

      

+3


source


df.query('1 <= ID <= 1233').loc['RowX', 'ValueX']

RowX    255.0
RowX    255.0
RowX    255.0
Name: ValueX, dtype: float64

      



+4


source


filtered_df = df[(df.ID.between(1, 1233)) & (df.index == 'RowX')][['ValueX']]

values_list = filtered_df.ValueX.tolist()

      

0


source







All Articles