第 1 部分:系列和数据框
如何安装
pip install pandas
如何使用图书馆
import pandas as pd
系列
数据框
创建一个系列
1.直接地
ser = pd.Series([10, 20, 30, 40])type(ser)>> ser.index>> RangeIndex(start=0, stop=4, step=1)
2. 标签系列索引
grades = ['A', 'B+', 'A+', 'A']ser = pd.Series(grades, index=['sam', 'june', 'max', 'july'])print(ser[0], ser[2]) # access with int indexprint(ser['sam'], ser['max']) # access with label>> A A+ser.index>> Index(['sam', 'june', 'max', 'july'], dtype='object')
3.字典到系列
grades = {'sam':'A', 'june':'B+', 'max':'A+', 'july':'A'}ser = pd.Series(grades)
创建一个数据框
1.无列标签
import pandas as pdimport numpy as npdf = pd.DataFrame(np.zeros((4,3)))type(df)>>
2.带列标签
import pandas as pdimport numpy as npclasses = ['science', 'english', 'math']df = pd.DataFrame(np.zeros((4,3)), columns = classes)
3. 使用字典和列表
import pandas as pdnames = ['sam', 'june', 'max', 'july']korean = [100, 95, 90, 85]english = [85, 90, 95, 100]math = [90, 85, 100, 95]df = pd.DataFrame({'name':names, 'korean':korean, 'english':english, 'math':math})
4. 标签的行索引
import pandas as pdnames = ['sam', 'june', 'max', 'july']korean = [100, 95, 90, 85]english = [85, 90, 95, 100]math = [90, 85, 100, 95]df = pd.DataFrame({'korean':korean, 'english':english, 'math':math}, index=names)
访问数据框
1.系列的列索引
# continuedname_ser = df['name']type(name_ser)>>
2. DataFrame 的列索引
# continuedclasses_df = df[['name', 'math']]type(classes_df)>>
3.广播
# continueddf['english'] = df['english'] + 10
4. 获得一排
# continuedprint(df[1:2])print(df[2:])
5. 获取元素
# Continued# Column - Row# Elementprint(df['korean'][1])# Row - Column# Seriesprint(df[1:2]['korean'])
6. 布尔索引
# continued (4) Row Index to Label bool_index = df['math'] > 90print(bool_index)print(df[bool_index])
bool_index = df > 90print(bool_index)print(df[bool_index])
DataFrame 高级索引
df.loc[row index]df.loc[rwo index, column index]df.loc[row slicing]df.loc[boolean list]
forloc 高级索引示例
# continued (4) Row Index to Labeldf.loc['sam']
df.loc[['sam', 'max']]df.loc['sam', 'korean']
df.loc['june':'july'] # includes july! df.loc[df['korean']>90]
df.loc[['sam', 'june'],['math','english']]df.loc[df['math']>90,['korean','english']]
iloc 高级索引的示例
# continued (4) Row Index to Labeldf.iloc[0]df.iloc[[0,2]]df.iloc[0,0]
df.iloc[0:2] # does NOT include max! df.iloc[0:2, 1:3]df.iloc[[0,3],[1,2]]
df.iloc[0]>90df.iloc[0][df.iloc[0]>90]# sam's subject over 90
关注七爪网,获取更多APP/小程序/网站源码资源!
留言与评论(共有 0 条评论) “” |