pandas
DataFrame
import pandas as pd
from pandas import Series, DataFrame
data = {"name":["yahoo","google","facebook"], "marks":[200,400,800], "price":[9, 3, 7]}
f1 = DataFrame(data)
print(f1)
marks name price
0 200 yahoo 9
1 400 google 3
2 800 facebook 7
# 按指定順序排列,并添加索引
f2 = DataFrame(data, columns = ['price', 'name', 'marks', 't'], index = ['a', 'b', 'c'])
print(f2)
price name marks t
a 9 yahoo 200 NaN
b 3 google 400 NaN
c 7 facebook 800 NaN
# 精準(zhǔn)輸出
print(f2['name']['a']) #yahoo
讀取csv
import pandas as pd
from pandas import Series, DataFrame
marks = pd.read_csv('marks.csv')
print(marks)
讀取excel
xlsx = pd.ExcelFile('.\marks.xlsx')
print(xlsx.sheet_names)
sheet_1 =xlsx.parse('Sheet1')
print(sheet_1)
['Sheet1', 'Sheet2', 'Sheet3', 'Sheet4', 'Sheet5', 'Sheet6']
name physics python math english
0 Google 100 100 25 12
1 Facebook 45 54 44 88
2 Twitter 54 76 13 91
3 Yahoo 54 452 26 100
numpy
import numpy as np
x = [12,4,2,4,23,121]
y = [32,21,223,43,12,55]
nx = np.array(x)
ny = np.array(y)
print(nx/ny**2)
#數(shù)據(jù)篩選就這么簡(jiǎn)單
print(nx[nx>12])#[ 23 121]
# 二維數(shù)組
x = [12,43,23,42,23,11]
y = [32,21,23,43,12,55]
np_2d = np.array([x,y])
print(np_2d)
# 輸出個(gè)坐標(biāo)值
print(np_2d[0,1])
# 輸出第二行
print(np_2d[1])
# 切片輸出(含前不含后)
print(np_2d[:,1:3])
print(np_2d[1:,1:2])