Quickly grasp the common operations of Data and Frame in Python

  • 2021-10-16 02:19:15
  • OfStack

Directory to master the common operations of Data Frame 1. View the common properties of DataFrame
2. Check, modify, add and delete DataFrame data 3. Describe and analyze DataFrame data

Master the common operation of Data and Frame

1. View common properties of DataFrame

The basic attributes of DataFrame are: values (element), index (index), columns (column name), dtypes (type), size (number of elements), ndim (dimension), shape (shape size), and transposition using T attribute


import pandas as pd
detail=pd.read_excel('E:\data\meal_order_detail.xlsx') # Read the data, using the read_excel  Function call 
# print(detail)
print(" Index ",detail.index)
print(" So   Value   : ",detail.values)
print(" So column names: ",detail.columns)
print(" Data type: ",detail.dtypes)
print(" Number of elements: ",detail.size)
print(" Dimensions: ",detail.ndim)
print(" Shape and size   Dimensions: ",detail.shape)
# Use T Attribute   Transpose 
print(" Shape before transposition: ",detail.shape) Data 
print(" Transposed shape: ",detail.T.shape)

2. Check, change, add and delete DataFrame data

View data accessing DataFramezhon '
(1.1) Basic Viewing of DataFrame Data


# Use dictionary access 
order_id=detail['order_id']
print(" Of the order details table order_id Shape of: ",order_id.shape)
# Use the way to access properties  
dishes_name=detail.dishes_name
print(" In the order details table dishes_name Shape of: ",dishes_name.shape)
#DataFrame  Data Acquisition with Single Column and Multiple Rows 
dishes_name5=detail['dishes_name'][:5]
print(dishes_name5)
# Multi-column and multi-row data 
orderDish=detail[['order_id','dishes_name']][:5]
print(orderDish)
# Access to multiple rows of data 
order5=detail[:][1:6]
print(" In the order details table 1~6 Data for row elements: \n",order5)

# Use DataFrame Adj. head And tail Method to get multiple rows of data 
print(' Before the order details table 5 Row data: \n',detail.head())#head() If there are no parameters inside, the default is 5 Row 
print(' After the order details table, 5 Row data: \n',detail.tail()) #tail() If there are no parameters inside, the default is 5 Row 

(1.2). loc and iloc access modes of DataFrame;


dishes_name1=detail.loc[:,'dishes_name'] #DataFrame.loc[ Row index name or condition , Column index name ]
print(" Use loc Extraction dishes_name Column size : ",dishes_name1.size)
dishes_name2=detail.iloc[:,3] #DataFrame.iloc[ Row index position , Column index position ]
print(" Use iloc Extracting number 3 Column size : ",dishes_name2.size)

# Use loc , iloc  Implement multi-column slicing 
orderDish1=detail.loc[:,['order_id','dishes_name']]
print(orderDish1.size)
orderDish2=detail.iloc[:,[1,3]]
print(orderDish2.size)
# Use loc , iloc  Achieve fancy slicing 
print(" Column name is order_id And dishes_name  The line name of is 3 Data of: \n",detail.loc[3,['order_id','dishes_name']])
print(' Column name is order_id And dishes_name  The line name is 2 , 3 , 4 , 5 , 6 The data of is: \n',detail.loc[2:6,['order_id','dishes_name']])
print(' Column name 1 And 3 The row position is 3 The data of is: \n',detail.iloc[3,[1,3]]) # Why not here loc Function, 
               # Because loc Function passes in the name of the column index (or the name or condition of the row), and iloc What is passed in is the location 
print(' Column position is 1 And 3 The row position is 2 , 3 , 4 , 5 , 6 And: \n',detail.iloc[2:7,[1,3]])# Here is the location index, 7 You can't get it 
# Use loc And iloc Function implementation conditional slicing 
print('detail Medium order_id For 458 Adj. dishes_name Is: \n',detail.loc[detail['order_id']==458,['order_id','dishes_name']]) # Used loc
print("detail Medium order_id For 458  The first part of 1 , 5 The data of the column is: \n",detail.iloc[(detail['order_id']==458).values,[1,5]])#values  Get Element  # Use iloc Function 

(1.3). ix slicing method


# Use loc , iloc , ix  Implementation slicing   Compare ( DataFrame.ix[ Index or position or condition of row , Column index name and location ] ) 
print(' Column name is dishes_name The line name is 2 , 3 , 4 , 5 , 6 The data of is: \n',detail.loc[2:6,['dishes_name']])
print(' Column position is 5 The line name is 2~6 The data of is: \n',detail.iloc[2:6,5])
print(' Column position is 5 The line name is 2~6 The data of is: \n',detail.ix[2:6,5])

2. Change data in DataFame


# Will order_id For 458  Change to  45800
detail.loc[detail['order_id']==458,'order_id'] = 45800 #45800  Here   Without single quotation marks 
print(' After the change detail In order_id For  458  Of: \n',detail.loc[detail['order_id']==458,'order_id'])
print(' After the change detail In order_id For  45800  Of: \n',detail.loc[detail['order_id']==45800,'order_id'])
detail.loc[detail['order_id']==45800,'order_id'] = 458

3. Adding data to DataFrame


# Add 1 Column unfixed value 
detail['payment']=detail['counts']*detail['amounts']
print('detail New column payment Before the 5 The row data is: \n',detail['payment'].head())
# Add 1 Column fixed value 
detail['pay_way']=' Cash payment '
print('detail Before adding a column 5 The data of the row is: \n',detail['pay_way'].head())
``4. Delete data from a row or column (drop)
# Delete a column 
print(' Delete pay_way Front  detail The column index in: \n',detail.columns)
detail.drop(labels='pay_way',axis=1,inplace=True)
print(' Delete pay_way Posterior  detail The column index in: \n',detail.columns)
# Delete certain lines 
print(' Delete 1~10 Row   Front  detail Length of: ',len(detail))
detail.drop(labels=range(1,11),axis=0,inplace=True)
print(' Delete 1~10 Row   Posterior  detail Length of: ',len(detail))

3. Describe and analyze DataFrame data

1. Descriptive statistics of numerical characteristics
Descriptive statistics of describe () function
2. Descriptive statistics of category characteristics
object type, categroy type


Related articles: