I've created DataFrame using pandas.
I'm Showing Map with custom Markers Using these dataframe.
I want to filter the Map on the basis of frontend side filter selection of dropdown Product_Type.
when I was Selecting any of these 2 Product_Type Options (Bag OR Bulk) from the Product_Type dropdown then its showing proper result.
but The Problem I was facing, when I was selecting Select All Option then its giving an error only list-like objects are allowed to be passed to isin(), you passed a [str]
.
In the Dataframe the Product_Type contains in list. How do I get All Values for Product_Type?
Here is the html file code:
<form action="" method="post">
{% csrf_token %}
<div class="row">
<div class="col-md-2 pl-0">
<label for="select_routeid_plant" class="mb-2">Plant Code</label>
<select name="plant_code" id="select_routeid_plant" class="selectedVal custom-select custom-select-sm">
<option value="all">Select All</option>
</select>
</div>
<div class="col-md-2 pl-0">
<label for="select_routeid_destination" class="mb-2">Destination</label>
<select name="destination_code" id="select_routeid_destination" class="selectedVal custom-select custom-select-sm">
<option value="all">Select All</option>
</select>
</div>
<div class="col-md-2 pl-0">
<label for="select_product_type" class="mb-2">Product Type</label>
<select name="product_type" id="select_product_type" class="selectedProductType custom-select custom-select-sm">
<option value="all">Select All</option>
</select>
</div>
<div class="col-md-2 pl-0">
<label for="select_month" class="mb-2">Month</label>
<select name="month" id="select_month" class="selectedProductType custom-select custom-select-sm">
<option value="all">Select All</option>
</select>
</div>
<input type="submit" class="btn btn-primary px-4 mt-4" value="Filter" onclick="showRoute()">
</div>
</form>
Here is the Views.py code
if request.method == "POST":
plant_code = request.POST.get("plant_code")
destination_code = request.POST.get("destination_code")
product = request.POST.get("product_type")
if plant_code == 'all':
plant_code = list(file_data['Route_Id'].unique())
else:
plant_code = plant_code.split("*")
if destination_code =='all':
destination_code = list(file_data['Route_Id'].unique())
else:
destination_code = destination_code.split("*")
if product =='all':
product = list(file_data['Product_Type'].unique())
else:
product = product.split("*")
post_data = file_data[(file_data['Route_Id'].isin(plant_code)) &
(file_data['Product_Type'].isin(product)) &
(file_data['Route_Id'].isin(destination_code))]
created the following dataframe:
import pandas as pd
df = pd.DataFrame({'A':[1, 2, 3, 4, 5], 'B':[['Bag'], ['Bag'], ['Bag', 'Bulk'], ['Bag'], ['Bag']]})
Output
A B
0 1 [Bag]
1 2 [Bag]
2 3 [Bag, Bulk]
3 4 [Bag]
4 5 [Bag]
when printing the first value of column B I get the type:
print(type(df.loc[0, 'B']))#<class 'list'>
you have to check your type:
print(type(file_data.loc[0, 'Product_Type']))#??????
According to the isin documentation, a list with values is substituted into the function.
Since in my example this is a list, I additionally enclose it in square brackets:
print(df['B'].isin([['Bag', 'Bulk']]))
Output
0 False
1 False
2 True
3 False
4 False
If I try to do something like list(file_data['Product_Type'].unique())
with my df['B'].unique()
column, it will fail with an error: TypeError: unhashable type: 'list'
. That is, you need to understand what file_data['Product_Type'].unique()
returns?
The same error that you get occurs when passing a string to isin instead of a list:print(df['B'].isin('Bag')):
TypeError: only list-like objects are allowed to be passed to isin(), you passed a [str]