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

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

6 Comments

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 - 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.
your data example is correct. Its just based on multiple column values i need to update a column in df
@Geek - hmm, then is better first solution, df.loc[m, 'D'] = 10
So m = np.logical_and.reduce([df[a] == b for a, b in (zip(dynamic_cols, dynamic_values))]) is not possible use?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.