SoFunction
Updated on 2025-03-02

Common operating methods of python dataframe: implement rows, columns, slices, and statistical feature values

Examples are as follows:

# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd
from pandas import *
from numpy import *

data = DataFrame((16).reshape(4,4),index = list("ABCD"),columns=list('wxyz'))
print data
print data[0:2] #Get the first two rows of dataprint'+++++++++++++'

print len(data )  #Find out how many rows in totalprint  #Find out how many columns in totalprint'+++++++++++++'

print  #Column Index Nameprint  #Line Index Nameprint'+++++++++++++'

print [1]  #Get the second row of dataprint [1]  #Get the second row of dataprint'+++++++++++++'

print data['x'] #Take a column of data with column index xprint ['A'] #Take a row of data with the index of the first row as "A",print'+++++++++++++'

print [:,['x','z'] ]  # means select all rows and columns with columns a and b;print [['A','B'],['x','z']] # indicates the union of the two rows 'A' and 'B' and columns with columns x and z;print'+++++++++++++'

print [1:3,1:3]  #Data slice operation, cut continuous data blocksprint [[0,2],[1,2]]  # That is, you can freely select the data corresponding to the column position and cut into scattered data blocksprint'+++++++++++++'

print data[data>2] # indicates that data is greater than 0 in the datasetprint data[>5] # means select all rows with column x in the dataset that are greater than 5
print'+++++++++++++'
a1=()
print a1[a1['y'].isin(['6','10'])] #Table display satisfies the condition: the value in column y contains all rows of '6', '8'.
print ()  #By default, find the average value for each column; if parameter (1) is added, find the average value for each row;print data['x'].value_counts() #Count the number of times each value appears in a column x:
print () # Statistics on each column of data, including count, mean, std, each quantile, etc.
data.to_excel(r'E:\pypractice\Yun\doc\',sheet_name='Sheet1') #Data output toExcel

The above common operation methods for python dataframe: implementing rows, columns, slices, and statistical feature values ​​is all the content I share with you. I hope you can give you a reference and I hope you can support me more.