Return Row Number(S) for a Particular Value in a Column in a Dataframe

Return row number(s) for a particular value in a column in a dataframe

Use which(mydata_2$height_chad1 == 2585)

Short example

df <- data.frame(x = c(1,1,2,3,4,5,6,3),
y = c(5,4,6,7,8,3,2,4))
df
x y
1 1 5
2 1 4
3 2 6
4 3 7
5 4 8
6 5 3
7 6 2
8 3 4

which(df$x == 3)
[1] 4 8

length(which(df$x == 3))
[1] 2

count(df, vars = "x")
x freq
1 1 2
2 2 1
3 3 2
4 4 1
5 5 1
6 6 1

df[which(df$x == 3),]
x y
4 3 7
8 3 4

As Matt Weller pointed out, you can use the length function.
The count function in plyr can be used to return the count of each unique column value.

How to get row number in dataframe in Pandas?

Note that a dataframe's index could be out of order, or not even numerical at all. If you don't want to use the current index and instead renumber the rows sequentially, then you can use df.reset_index() together with the suggestions below

To get all indices that matches 'Smith'

>>> df[df['LastName'] == 'Smith'].index
Int64Index([1], dtype='int64')

or as a numpy array

>>> df[df['LastName'] == 'Smith'].index.to_numpy()  # .values on older versions
array([1])

or if there is only one and you want the integer, you can subset

>>> df[df['LastName'] == 'Smith'].index[0]
1

You could use the same boolean expressions with .loc, but it is not needed unless you also want to select a certain column, which is redundant when you only want the row number/index.

I want to get row numbers of all rows in a dataframe where an element in a column contains an element in a vector

Most directly, you can use charmatch.

charmatch(vec, df$cat)
[1] 4 5 3 1 2

or return a named vector with grep and sapply.

sapply(Categories, grep, df$cat, fixed=TRUE)
d- e- c- a- b-
4 5 3 1 2

For a little semantic sugar, build your own function with Vectorize:

vecGrep <- Vectorize(grep, vectorize.args="pattern")

Then use it to return a named vector

vecGrep(Categories, df$cat, fixed=TRUE)
d- e- c- a- b-
4 5 3 1 2

data

vec <- c("d-", "e-", "c-", "a-", "b-")
df = data.frame(name = c(1:10), cat = paste(c(letters[1:10]), "-1", sep = ""))

Pandas get cell value by row NUMBER (NOT row index) and column NAME

df['Age'].iloc[0] works too, similar to what Chris had answered.

Pandas - For a given column value in a row return the value from the column which name matches the value

You can use pandas 'fancy indexing' lookup, which pairs the index and the column names and pick up one value for each pair; and in this case it will be for each index(row), it picks up the value under the corresponding column from col_D:

df = pd.DataFrame({'col_A': [1,2,3], 'col_B': [2,3,0], 'col_D': ['col_B', 'col_B', 'col_A']})

df['col_C'] = df.lookup(df.index, df.col_D)

df
# col_A col_B col_D col_C
#0 1 2 col_B 2
#1 2 3 col_B 3
#2 3 0 col_A 3

Find row number in column where it matches any other value in column of other dataframe

Try isin and slicing on index

a_index = (redcap_final_arm1_data.index[redcap_final_arm1_data.record_id
.isin(arm_1_and_m1_df.record_id)].tolist())

output:

Out[1355]: [0, 2, 3, 9]

How to get excel cell/row number of a value using python?

Keep in mind that the index of pandas dataframes is zero-based and the row numbers of excel worksheets are one-based, therefore, if you would like to solve this issue with pandas

df["index"] = df.index.astype(int) + 1

is correct. However, note that the picture in your question shows a part of an excel worksheet, which has an active filter applied. The solution presented here works only, if the data in excel is not filtered.

Update to respond to the first comment

You can also specify values in .isin and get their location in terms of index and column with the following code (not pretty):

import pandas as pd

df = pd.DataFrame((
(2, 5, 8),
(20, 5, 772),
(0, 20, 5),
(2, 0, 0),
(76, 35, 66),
(23, 21, 29),
(5, 55, 88),
(44, 23, 5),
(1, 2, 3),
), columns=["a", "b", "c"])

cols = df.columns.to_list()
for col in cols:
filt = df[col].isin([5, 2])
df_x = df.loc[filt]
for row, vals in df_x.iteritems():
for val in vals.index:
print(f"index:{val:4} column: {col}")


Related Topics



Leave a reply



Submit