hello_python_world #4: filtering dataframe

A subset is an extract for the data table or data frame base on a condition. We use filters to create subset of the data and the condition is a logical expression. Some examples of subset are: sales from an specific store - data['store_column_name'] == 'store01' – this logic could translate as: show me the the lines where store_column_name are equal to 'store01'. Also we can create another subset with stores with sales greater than 1 million – data['store_sales_column'] > 1000000


Concepts and inputs

We need to define a logical expression to filter a data frame. To build the logical expression we use logical operators: greater than '>' , less than '<', equal to '==', not equal '!='. The logical operator is used to compare two variables, a > b – means a greater than b – and so on. Be aware that the logical expressions works for the same type of variables, if you try to compare '6' == 6, the result will be False, because we are trying to compare a string vs number.


Solution

The variable mask is defined as a condition, we would like to subset sells from store B1. So, the condition will be mask

No alt text provided for this image

Code

No alt text provided for this image

Comments

Filter conditions can be multiples, if we want to filter the data base on item and store, just create two different condition and then connect it using the operator '&' (and).


要查看或添加评论,请登录

Luciano Bracho的更多文章

  • hello_python_world #3: move or copy a file

    hello_python_world #3: move or copy a file

    Data transformation is the process to change, add and or remove data to a file. Is very common to transform your…

  • hello_python_world #2: Aggregation / Groupby

    hello_python_world #2: Aggregation / Groupby

    We had a file with more than 5 million registers and we wanted to see the sales by item and save it in a csv file…

  • hello_python_world #1: Wide to Long

    hello_python_world #1: Wide to Long

    Was required to transform a wide excel table to a csv long table once per month. Concepts and Inputs Let’s start with…

    3 条评论

社区洞察

其他会员也浏览了