I have a collection called project, this collection contain different documents, and every document contain an array of object called data.
I want to be able to filter the data (Excel files) by projectAlias and use pymongon and pandas to structure this file in SQL (Columns and row)
For instance
One can use filter() function in JavaScript to filter the object array based on attributes. The filter() function will return a new array containing all the array elements that pass the given condition. If no elements pass the condition it returns an empty array.
Filter() is a built-in function in Python. The filter function can be applied to an iterable such as a list or a dictionary and create a new iterator. This new iterator can filter out certain specific elements based on the condition that you provide very efficiently.
filter() method is a very useful method of Python. One or more data values can be filtered from any string or list or dictionary in Python by using filter() method. It filters data based on any particular condition. It stores data when the condition returns true and discard data when returns false.
There's no code here, so I have to make some guesses:
Something like this might be useful:
#Made up function for first and third assumptions
data_array = collect_data(documents)
data_array.sort(key=lambda obj: obj["projectAlias"])
#Or, to create a new array with sorted data
sorted_data = sorted(data_array, key=lambda obj: obj["projectAlias"])
The key arg for python's built in sort function takes some sort of other function and runs it on each element of the array before sorting the results of that array. Then because python is helpful, it has predefined comparisons of strings for sorting which puts capital letters first then lowercase for the English alphabet. That changes when you get into accents, umlauts, and other variations. I have no insight there.
If your data needs some other sorting, you would want to define a different lambda function for key that results in outputs that fit your desired sort more. Another one could be by the length of the value:
#Made up function for first and third assumptions
data_array = collect_data(documents)
data_array.sort(key=lambda obj: len(obj["projectAlias"]))
#Or, to create a new array with sorted data
sorted_data = sorted(data_array, key=lambda obj: len(obj["projectAlias"]))
If you want more info, the "Key Function" section here in python.org's wiki might be useful
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