hope some one can help me.
Given one value per column, I succeeded in filtering a Pandas Dataframe (as shown in code below). However, depending on the analysis I am running, sometimes I would like to avoid specifying a value to filter for (for example, I would like to ignore the filtering by seg_device and filter the dataframe based on the os only).
However, the code below is forcing me to always specify some value (e.g. desktop). If I leave seg_device blank, df_ch_seg will return no data, given the condition df_ch.device == seg_device.
Would someone have any advice on how to make my code more flexible? My dataset is made of 1 million rows, per 16 columns. Below you see only 2 filters, but I have 15 in total (some of them are integers, some are strings columns). Thank you!
By looking at the code below, I would like to slightly change it so that it works in multiple occasions:
- if I want to filter by one device (e.g. mobile)
- if i want to filter by 2 device (e.g. mobile, desktop)
- if I don't want to filter by device (I would like my code to ignore the filter by device)
# [...]
seg_device = input('Enter device (e.g. desktop, ...): ')
seg_os = input('Enter operating system (e.g. Mac/iOS, Windows, ...): ')
# [...]
# Define new dataframe df_ch_seg, based on df_ch, segmented based on above input values
df_ch_seg = df_ch[(df_ch.device == seg_device)& (df_ch.os == seg_os)]