python - Checking which rows contain a value efficiently -
i trying write function checks presence of value in row across columns. have script iterating through columns, worried inefficient when used on large datasets.
here current code:
import pandas pd = [1, 2, 3, 4] b = [2, 3, 3, 2] c = [5, 6, 1, 3] d = [1, 0, 0, 99] df = pd.dataframe({'a': a, 'b': b, 'c': c, 'd': d}) cols = ['a', 'b', 'c', 'd'] df['e'] = 0 col in cols: df['e'] = df['e'] + df[col] == 1 print(df)
result:
b c d e 0 1 2 5 1 true 1 2 3 6 0 false 2 3 3 1 0 true 3 4 2 3 99 false
as can see, column e keeps record of whether value "1" exists in row. wondering if there better/more efficient way of achieving these results.
you can check if values in data frame 1 , see if true in row (with axis=1):
df['e'] = df.eq(1).any(1) df # b c d e #0 1 2 5 1 true #1 2 3 6 0 false #2 3 3 1 0 true #3 4 2 3 99 false
Comments
Post a Comment