Professional Documents
Culture Documents
df = pd.read_csv("../global_sales_data/market_fact.csv")
df.head()
Out[10]: Ord_id Prod_id Ship_id Cust_id Sales Discount Order_Quantity Profit Shipping_
Arguably, the best way to do these operations is using df.loc[] , since df.iloc[] would
require you to remember the integer column indices, which is tedious.
http://localhost:8888/notebooks/Desktop/Biplab/My%20Python/Pandas/Introduction_to_Pandas/4_Slicing_Dicing.ipynb 1/16
1/4/2019 4_Slicing_Dicing
Out[11]: 0 False
1 False
2 True
3 False
4 True
5 False
6 False
7 True
8 False
9 False
10 True
11 False
12 False
13 True
14 False
15 False
16 False
17 True
18 False
19 False
20 False
21 False
22 True
23 False
24 False
25 False
26 False
27 False
28 True
29 False
...
8369 False
8370 False
8371 True
8372 False
8373 False
8374 False
8375 False
8376 False
8377 False
8378 False
8379 False
8380 False
8381 False
8382 False
8383 True
8384 False
8385 False
8386 False
8387 False
8388 False
8389 False
http://localhost:8888/notebooks/Desktop/Biplab/My%20Python/Pandas/Introduction_to_Pandas/4_Slicing_Dicing.ipynb 2/16
1/4/2019 4_Slicing_Dicing
8390 False
8391 False
8392 False
8393 False
8394 False
8395 False
8396 False
8397 True
8398 False
Name: Sales, Length: 8399, dtype: bool
http://localhost:8888/notebooks/Desktop/Biplab/My%20Python/Pandas/Introduction_to_Pandas/4_Slicing_Dicing.ipynb 3/16
1/4/2019 4_Slicing_Dicing
http://localhost:8888/notebooks/Desktop/Biplab/My%20Python/Pandas/Introduction_to_Pandas/4_Slicing_Dicing.ipynb 4/16
1/4/2019 4_Slicing_Dicing
http://localhost:8888/notebooks/Desktop/Biplab/My%20Python/Pandas/Introduction_to_Pandas/4_Slicing_Dicing.ipynb 5/16
1/4/2019 4_Slicing_Dicing
http://localhost:8888/notebooks/Desktop/Biplab/My%20Python/Pandas/Introduction_to_Pandas/4_Slicing_Dicing.ipynb 6/16
1/4/2019 4_Slicing_Dicing
http://localhost:8888/notebooks/Desktop/Biplab/My%20Python/Pandas/Introduction_to_Pandas/4_Slicing_Dicing.ipynb 7/16
1/4/2019 4_Slicing_Dicing
Out[14]: Ord_id Prod_id Ship_id Cust_id Sales Discount Order_Quantity Profit Shi
http://localhost:8888/notebooks/Desktop/Biplab/My%20Python/Pandas/Introduction_to_Pandas/4_Slicing_Dicing.ipynb 8/16
1/4/2019 4_Slicing_Dicing
http://localhost:8888/notebooks/Desktop/Biplab/My%20Python/Pandas/Introduction_to_Pandas/4_Slicing_Dicing.ipynb 9/16
1/4/2019 4_Slicing_Dicing
In [15]: # The 'OR' operator is represented by a | (Note that 'or' doesn't work with pandas
# E.g. all orders having 2000 < Sales OR Profit > 100
df.loc[(df.Sales > 2000) | (df.Profit > 100), :]
http://localhost:8888/notebooks/Desktop/Biplab/My%20Python/Pandas/Introduction_to_Pandas/4_Slicing_Dicing.ipynb 10/16
1/4/2019 4_Slicing_Dicing
http://localhost:8888/notebooks/Desktop/Biplab/My%20Python/Pandas/Introduction_to_Pandas/4_Slicing_Dicing.ipynb 11/16
1/4/2019 4_Slicing_Dicing
In [16]: # E.g. all orders having 2000 < Sales < 3000 and Profit > 100
# Also, this time, you only need the Cust_id, Sales and Profit columns
df.loc[(df.Sales > 2000) & (df.Sales < 3000) & (df.Profit > 100), ['Cust_id', 'Sal
http://localhost:8888/notebooks/Desktop/Biplab/My%20Python/Pandas/Introduction_to_Pandas/4_Slicing_Dicing.ipynb 12/16
1/4/2019 4_Slicing_Dicing
http://localhost:8888/notebooks/Desktop/Biplab/My%20Python/Pandas/Introduction_to_Pandas/4_Slicing_Dicing.ipynb 13/16
1/4/2019 4_Slicing_Dicing
http://localhost:8888/notebooks/Desktop/Biplab/My%20Python/Pandas/Introduction_to_Pandas/4_Slicing_Dicing.ipynb 14/16
1/4/2019 4_Slicing_Dicing
http://localhost:8888/notebooks/Desktop/Biplab/My%20Python/Pandas/Introduction_to_Pandas/4_Slicing_Dicing.ipynb 15/16
1/4/2019 4_Slicing_Dicing
In [18]: # You may want to select rows whose column value is in an iterable
# For instance, say a colleague gives you a list of customer_ids from a certain re
# To get all the orders from these customers, use the isin() function
# It returns a boolean, which you can use to select rows
df.loc[df['Cust_id'].isin(customers_in_bangalore), :]
Out[18]: Ord_id Prod_id Ship_id Cust_id Sales Discount Order_Quantity Profit Ship
http://localhost:8888/notebooks/Desktop/Biplab/My%20Python/Pandas/Introduction_to_Pandas/4_Slicing_Dicing.ipynb 16/16