tushariyer
tushariyer

Reputation: 958

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

As the question says, I have a data frame which is quite large but looks like:

        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

What I want is to be able to select all the values in column ValueX where the row is RowX for each of the ID numbers 1-1233 and return them in a list.

Upvotes: 3

Views: 67

Answers (3)

Diego Mora Cespedes
Diego Mora Cespedes

Reputation: 3872

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

values_list = filtered_df.ValueX.tolist()

Upvotes: 0

piRSquared
piRSquared

Reputation: 294488

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

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

Upvotes: 4

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210882

IIUC:

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

Upvotes: 3

Related Questions