I have the following given:
TR Avg RE
3 0
3 0
4 209.3524872
3 185.6542898
3 0
3 0
3 0
4 136.7522375
4 157.6887675
4 0
3 202.8994858
3 0
3 89.45242983
4 0
3 0
3 218.4987273
3 192.4212849
I want to extract only those values of RE where TR is equal to 4 - how can I do that?
If you want to extract the unique values based on two conditions, here is another array formula can do you a favor, please do as this: 1. Enter the below formula into a blank cell where you want to list the unique values, in this example, I will put it to cell G2, and then press Shift + Ctrl + Enter keys to get the first unique value.
You can also select column values based on another DataFrame column value by using DataFrame.loc [] property. The .loc [] property explains how to access a group of rows and columns by label (s) or a boolean array.
You can extract a column of pandas DataFrame based on another value by using the DataFrame.query () method. The query () is used to query the columns of a DataFrame with a boolean expression. The blow example returns a Courses column where the Fee column value matches with 25000.
Select Text option from the Formula Type drop down list; Then choose Extract cells with unique values (include the first duplicate) from the Choose a fromula list box; In the right Arguments input section, select a list of cells that you want to extract unique values. 4.
You already have the answer from David's comment. However, for interest below is few additional methods to do it.
Code:
# Method 1:
df[df$TR == 4, "RE"]
# Method 2:
df[ which(df$TR == 4), "RE"]
# Method 3:
subset(df$RE, df$TR == 4)
# Method 4: You could also use the sqldf package to use sql
# install.packages("sqldf")
library(sqldf)
sqldf('select RE from df where TR = 4')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With