Python's equivalent of C# LINQ's select
Question:
I’m quite new to python, and happen to have used C# for some time now. I saw that there was a filter method to use with the collections, which seems to be the equivalent of the LINQ’s where clause.
I wondered, is there also an equivalent for the LINQ’s select statement in python?
Example: my_collection.select(my_object => my_object.my_property)
would return a collection of the my_property of each object in my_collection.
Answers:
You can use map(), but List Comprehensions are a more “pythonic” way of doing this.
[my_object.my_property for my_object in my_collection]
try pandas!
select
C# my_collection.Select(my_object => my_object.my_property)
pandas my_collection['my_property']
or:
C# my_collection.Select(x => x.my_property + 2)
python my_collection['my_property'].apply(lambda x: x + 2)
where
C#: my_collection.Where(x => x.my_property == 1)
pandas: my_collection[my_collection['my_property']==1]
I’m quite new to python, and happen to have used C# for some time now. I saw that there was a filter method to use with the collections, which seems to be the equivalent of the LINQ’s where clause.
I wondered, is there also an equivalent for the LINQ’s select statement in python?
Example: my_collection.select(my_object => my_object.my_property)
would return a collection of the my_property of each object in my_collection.
You can use map(), but List Comprehensions are a more “pythonic” way of doing this.
[my_object.my_property for my_object in my_collection]
try pandas!
select
C# my_collection.Select(my_object => my_object.my_property)
pandas my_collection['my_property']
or:
C# my_collection.Select(x => x.my_property + 2)
python my_collection['my_property'].apply(lambda x: x + 2)
where
C#: my_collection.Where(x => x.my_property == 1)
pandas: my_collection[my_collection['my_property']==1]