Create DataFrame with multiple conditions

Hello Streamlit Team ! I have an issue creating a new dataframe to draw a map.

Blockquote
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import altair as alt
import pydeck as pdk
from streamlit_option_menu import option_menu
df = pd.read_csv(“clean_taxi.csv”)
hour_to_filter = st.slider(‘hour’, 0, 23, 0)
day_to_filter = st.slider(‘day’, 2, 8, 0)
minute_to_filter = st.slider(‘minute’, 0, 59, 0)
filtered_data = df(df[df.hour == hour_to_filter & df.day == day_to_filter & df.minute == minute_to_filter])
st.map(filtered_data)

I don’t know how to create the filtered_data with those 3 conditions.
Thanks you very much !

Hi, I know there are multiple ways to filter a Pandas dataframe, but for some reason I always use the .loc command:

filtered_data = df.loc[(df.hour == hour_to_filter) & (df.day == day_to_filter) & (df.minute == minute_to_filter)]

I’ll usually then reset the index of the new dataframe

filtered_data.reset_index(inplace=True, drop=True)

Does that do what you wanted?

EDIT: Change a set of round brackets to square ones

Hey ! Thanks you very much it works :slight_smile:

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.