Generate data table

<>1, Import first pandas library , It's usually used numpy library , So let's import the spare first :

import numpy as np 
import pandas as pd

<>2, Import CSV perhaps xlsx file :

data = pd.read_csv(‘name.csv’,header=1)   The first one is read by default sheet

data = pd.read_csv(‘name.csv’,sheet_name='sheetName') according to sheet Name acquisition sheet

data = pd.read_csv(‘name.csv’,sheet_name=None)
Get all sheet,data.keys() Get all sheet_name list .

3 use pandas Create data table :
df = pd.DataFrame({"id":[1001,1002,1003,1004,1005,1006],
"date":pd.date_range('20130102', periods=6), "city":['Beijing ', 'SH', '
guangzhou ', 'Shenzhen', 'shanghai', 'BEIJING '], "age":[23,44,54,32,34,32],
"category":['100-A','100-B','110-A','110-C','210-A','130-F'],
"price":[1200,np.nan,2133,5433,np.nan,4432]}, columns
=['id','date','city','category','age','price'])
4: Reads the specified single line , The data will be stored in the list
#1: Read the specified line df=pd.read_excel('lemon.xlsx')# This will be read directly to this by default Excel The first form of
data=df.ix[0].values#0 Indicates the first line The data read here does not include the header , Pay attention !
print(" Reads the data of the specified row :\n{0}".format(data)) On top ix Has been abandoned , Replace it with the one below df.loc[:, ['B', 'A'] perhaps
df.iloc['a', 'b']
2: Reads the specified multiple lines , Data will be stored in nested lists :
df=pd.read_excel('lemon.xlsx')
data=df.ix[[1,2]].values# Read the specified multiple lines , It's going to be here ix[] The nested list specifies the number of rows
print(" Reads the data of the specified row :\n{0}".format(data))
3: Reads the specified row and column :
df=pd.read_excel('lemon.xlsx') data=df.ix[1,2]# Read the value of the first row and second column , You don't need nested lists here
print(" Reads the data of the specified row :\n{0}".format(data))
4: Reads the specified multi row multi column value :
df=pd.read_excel('lemon.xlsx')
data=df.ix[[1,2],['title','data']].values# Read the value of the first line and the second line title as well as data The value of the column , Nested lists are needed here
print(" Reads the data of the specified row :\n{0}".format(data))
5: Gets the specified column for all rows
df=pd.read_excel('lemon.xlsx')
data=df.ix[:,['title','data']].values# Read all lines title as well as data The value of the column , Nested lists are needed here
print(" Reads the data of the specified row :\n{0}".format(data))
6: Get the line number and print it out
df=pd.read_excel('lemon.xlsx') print(" Output line number list ",df.index.values) The output is : Output line number list
[0 1 2 3]
7: Get the column name and print it out
df=pd.read_excel('lemon.xlsx') print(" Output column header ",df.columns.values) The operation results are as follows :
Output column header ['case_id' 'title' 'data']
8: Gets the value of the specified number of rows :

 
df=pd.read_excel('lemon.xlsx')
print(" Output value ",df.sample(3).values)# This approach is similar to head() Methods and df.values method Output value [[2 ' Enter the wrong password '
'{"mobilephone":"18688773467","pwd":"12345678"}'] [3 ' Normal recharge '
'{"mobilephone":"18688773467","amount":"1000"}'] [1 ' Normal login '
'{"mobilephone":"18688773467","pwd":"123456"}']]
 

9: Gets the value of the specified column :
df=pd.read_excel('lemon.xlsx') print(" Output value \n",df['data'].values)
 

Technology
Daily Recommendation