In pandas iloc loc take data difference and the method of value according to conditions in detail
- 2021-01-25 07:42:28
- OfStack
Dataframe uses loc to fetch rows and columns:
print(df.loc[0:4,['item_price_level','item_sales_level','item_collected_level','item_pv_level']])
The results are as follows, with 5 rows and 4 columns of index 0 to 4.
item_price_level item_sales_level item_collected_level item_pv_level
0 3 3 4 14
1 3 3 4 14
2 3 3 4 14
3 3 3 4 14
4 3 3 4 14
Instead of using iloc, it looks like this:
print(df.iloc[0:4,6:9])
The results are as follows, with index for rows 0 through 34, and 3 columns 6 through 8 (starting with column 0).
item_price_level item_sales_level item_collected_level
0 3 3 4
1 3 3 4
2 3 3 4
3 3 3 4
In addition, loc can fetch data according to conditions:
print(df.loc[df.item_price_level==0,:])
print(df.loc[df[item_price_level]==0,:])
item_price_level = 0 item_price_level = 0 item_price_level = 0 We can change the colon to the name of several columns, and select only the columns that meet our condition:
print(df.loc[df['item_price_level']==0,['item_price_level','item_sales_level']])
The first two lines of the result are as follows:
item_price_level item_sales_level
129141 0 10
129142 0 10
When the condition is multiple (both conditions are satisfied as follows) :
print(df.loc[(item_price_level==0) & (item_sales_level==3),:])