I want to filter pandas dataframe with values form dictionary
.
Here is explanation.
Let's say I have dataframe
like this. Data types are only string
, int
and timestamps
.
+---------+-------+------+-------+-----------------+
| Title | Type | Size | Price | Date |
+---------+-------+------+-------+-----------------+
| title 1 | red | 10 | 20 | 4.9.2020. 13:21 |
| title 2 | blue | 20 | 40 | 4.9.2020. 11:21 |
| title 3 | green | 15 | 50 | 4.5.2020. 13:21 |
+---------+-------+------+-------+-----------------+
and I have dictionary created from users input which looks like this:
userDict = {'Title':'title 1', 'Type':'red','Size':[10,15],'Price':[40, 80],
'Date':[Timestamp('2020-04-09 13:21:00'), Timestamp('2020-04-09 14:21:00')]}
'Price': [50, 80]
would mean that user is requesting all prices between 50 and 80.
How can I get filtered pandas dataframe based on values from dictionary?
Original dataframe has many other columns. Besides Title
and Type
I could have many other string
columns, and besides Size
and Price
I could also have many other int
columns.
So dictionary can be much larger containing filtering requests for many other fields.
My solution so far:
for key, value in userDict.items():
if 'list' in str(type(value)):
filteredDF= originalDF[(originalDF[key] > value[0]) & (originalDF[key] < value[1])]
if 'str' in str(type(value)):
filteredDF= filteredDF[filteredDF[key].str.contains(value, flags=re.IGNORECASE)]
But it keeps returning original dataframe.
extract price value like below,
start, end = userDict["Price"]
then filter from main dataframe like below
res = df[df["Price"],between(start, end)]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.