1

i have a dynamic situation, wherein i want to filter the data as per bellow logic.

dynamic_cols = ['A', 'B'. ...]
dynamic_values = [1,2,...]

data_frame.LOC[data_frame[dynamic_cols == dynamic_values ]]

i have used pandas, and numpy.

Any suggestion on this please?

Ex: In above case i want to filter the rows where in A=1 and Column B=2

Geek
  • 1,214
  • 5
  • 14
  • 27

1 Answers1

2

Use np.logical_and + reduce of all masks created by list comprehension or create helper DataFrame and merge:

df = pd.DataFrame({
         'A':[1,2,4,1,5,4],
         'B':[2,8,9,2,2,3],
         'C':[3,3,5,3,1,0],

})

print (df)
   A  B  C
0  1  2  3
1  2  8  3
2  4  9  5
3  1  2  3
4  5  2  1
5  4  3  0

dynamic_cols = ['A','B','C']
dynamic_values = [1, 2, 3]

m = np.logical_and.reduce([df[a] == b for a, b in (zip(dynamic_cols, dynamic_values))])
df1 = df[m]
print (df1)
   A  B  C
0  1  2  3
3  1  2  3
df2 = pd.DataFrame([dynamic_values], columns=dynamic_cols)
df1 = df.merge(df2)
print (df1)
   A  B  C
0  1  2  3
3  1  2  3
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Thank you for the quick response. It worked for me. But one more question..`pd.DataFrame([dynamic_values], columns=dynamic_cols)` if i want to update the column `D` in the main `df` how can i do it now – Geek Feb 01 '19 at 08:43
  • @Geek - Not sure if understand, because I dont know how looks data, is possible specify it more? E.g create data sample? Be free use a nd modify sample data from my answer. – jezrael Feb 01 '19 at 08:48
  • your data example is correct. Its just based on multiple column values i need to update a column in df – Geek Feb 01 '19 at 08:49
  • @Geek - hmm, then is better first solution, `df.loc[m, 'D'] = 10` – jezrael Feb 01 '19 at 08:52
  • yes this is the exact solution. When i know `m`. i.e if `m` is like `a=1`. But my case is dynamic, means in one scenario `m` is `a=1` in another `[a,b]=[1,1]` – Geek Feb 01 '19 at 08:54
  • 1
    So `m = np.logical_and.reduce([df[a] == b for a, b in (zip(dynamic_cols, dynamic_values))])` is not possible use? – jezrael Feb 01 '19 at 08:56