Pandas入门1(DataFrame+Series读写/Index+Select+Assign)
生活随笔
收集整理的這篇文章主要介紹了
Pandas入门1(DataFrame+Series读写/Index+Select+Assign)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 1. Creating, Reading and Writing
- 1.1 DataFrame 數據框架
- 1.2 Series 序列
- 1.3 Reading 讀取數據
- 2. Indexing, Selecting, Assigning
- 2.1 類python方式的訪問
- 2.2 Pandas特有的訪問方式
- 2.2.1 iloc 基于index訪問
- 2.2.2 loc 基于label標簽訪問
- 2.3 set_index() 設置索引列
- 2.4 Conditional selection 按條件選擇
- 2.4.1 布爾符號 `&,|,==`
- 2.4.2 Pandas內置符號 `isin,isnull、notnull`
- 2.5 Assigning data 賦值
- 2.5.1 賦值常量
- 2.5.2 賦值迭代的序列
learn from https://www.kaggle.com/learn/pandas
下一篇:Pandas入門2(DataFunctions+Maps+groupby+sort_values)
1. Creating, Reading and Writing
1.1 DataFrame 數據框架
- 創建DataFrame,它是一張表,內部是字典,key :[value_1,...,value_n]
- 字典內的value也可以是:字符串
- 給數據加索引index,index=['index1','index2',...]
1.2 Series 序列
- Series 是一系列的數據,可以看成是 list
- 也可以把數據賦值給Series,只是Series沒有列名稱,只有總的名稱
- DataFrame本質上是多個Series粘在一起
1.3 Reading 讀取數據
- 讀取csv("Comma-Separated Values")文件,pd.read_csv('file'),存入一個DataFrame
- 可以自定義索引列,index_col=, 可以是列的序號,或者是列的 name
(下圖比上面少了一列,因為定義了index列為0列)
- 保存,to_csv('xxx.csv')
2. Indexing, Selecting, Assigning
2.1 類python方式的訪問
item.col_name # 缺點,不能訪問帶有空格的名稱的列,[]操作可以 item['col_name'] wine_rev.country wine_rev['country']0 Italy 1 Portugal 2 US 3 US 4 US... 129966 Germany 129967 US 129968 France 129969 France 129970 France Name: country, Length: 129971, dtype: object wine_rev['country'][0] # 'Italy',先取列,再取行 wine_rev.country[1] # 'Portugal'2.2 Pandas特有的訪問方式
2.2.1 iloc 基于index訪問
-
要選擇DataFrame中的第一行數據,我們可以使用以下代碼:
-
wine_rev.iloc[0]
loc和iloc都是行第一,列第二,跟上面python操作是相反的
- wine_rev.iloc[:,0],獲取第一列,: 表示所有的
- wine_rev.iloc[:3,0],:3 表示 [0:3)行 0,1,2
- 也可以用離散的list,來取行,wine_rev.iloc[[1,2],0]
- 取最后幾行,wine_rev.iloc[-5:],倒數第5行到結束
2.2.2 loc 基于label標簽訪問
- wine_rev.loc[0, 'country'],行也可以使用 [0,1]表示離散行,列不能使用index
- wine_rev.loc[ : 3, 'country'],跟iloc不一樣,這里包含了3號行,loc包含末尾的
- wine_rev.loc[ 1 : 3, ['country','points']],多列用 list 括起來
- loc 的優勢,例如有用字符串 index 的行,df.loc['Apples':'Potatoes']可以選取
2.3 set_index() 設置索引列
- set_index() 可以重新設置索引,wine_rev.set_index("title")
2.4 Conditional selection 按條件選擇
2.4.1 布爾符號 &,|,==
- wine_rev.country == 'US',按國家查找, 生成了Series of True/False,可用于 loc
- wine_rev.loc[wine_rev.country == 'US'],把 US 的行全部選出來
- wine_rev.loc[(wine_rev.country == 'US') & (wine_rev.points >= 90)],US的&且得分90以上的
- 還可以用 | 表示或(像C++的位運算符號)
2.4.2 Pandas內置符號 isin,isnull、notnull
- wine_rev.loc[wine_rev.country.isin(['US','Italy'])],只選 US 和 Italy 的行
- wine_rev.loc[wine_rev.price.notnull()],價格不為空的
- wine_rev.loc[wine_rev.price.isnull()],價格為NaN的
2.5 Assigning data 賦值
2.5.1 賦值常量
- wine_rev['critic'] = 'Michael',新加了一列
- wine_rev.country = 'Ming',已有的列的value會直接被覆蓋
2.5.2 賦值迭代的序列
- wine_rev['test_id'] = range(len(wine_rev),0,-1)
下一篇:Pandas入門2(DataFunctions+Maps+groupby+sort_values)
總結
以上是生活随笔為你收集整理的Pandas入门1(DataFrame+Series读写/Index+Select+Assign)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode 1452. 收藏清单(
- 下一篇: LeetCode MySQL 1517.