3-2-Pandas 索引
Pandas章节应用的数据可以在以下链接下载: https://files.cnblogs.com/files/AI-robort/Titanic_Data-master.zip
In [4]:
import pandas as pd
df=pd.read_csv('./Titanic_Data-master/Titanic_Data-master/train.csv')
In [5]:
df['Age'][:5]
Out[5]:
0 22.0 1 38.0 2 26.0 3 35.0 4 35.0 Name: Age, dtype: float64
In [6]:
df[['Age','Fare']][:5]
Out[6]:
| Age | Fare | |
|---|---|---|
| 0 | 22.0 | 7.2500 |
| 1 | 38.0 | 71.2833 |
| 2 | 26.0 | 7.9250 |
| 3 | 35.0 | 53.1000 |
| 4 | 35.0 | 8.0500 |
- loc 用lable来定位
- iloc 用position来定位
In [8]:
df.iloc[0]
Out[8]:
PassengerId 1 Survived 0 Pclass 3 Name Braund, Mr. Owen Harris Sex male Age 22 SibSp 1 Parch 0 Ticket A/5 21171 Fare 7.25 Cabin NaN Embarked S Name: 0, dtype: object
In [9]:
df.iloc[0:5]
Out[9]:
| PassengerId | Survived | Pclass | Name | Sex | Age | SibSp | Parch | Ticket | Fare | Cabin | Embarked | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 1 | 0 | 3 | Braund, Mr. Owen Harris | male | 22.0 | 1 | 0 | A/5 21171 | 7.2500 | NaN | S |
| 1 | 2 | 1 | 1 | Cumings, Mrs. John Bradley (Florence Briggs Th... | female | 38.0 | 1 | 0 | PC 17599 | 71.2833 | C85 | C |
| 2 | 3 | 1 | 3 | Heikkinen, Miss. Laina | female | 26.0 | 0 | 0 | STON/O2. 3101282 | 7.9250 | NaN | S |
| 3 | 4 | 1 | 1 | Futrelle, Mrs. Jacques Heath (Lily May Peel) | female | 35.0 | 1 | 0 | 113803 | 53.1000 | C123 | S |
| 4 | 5 | 0 | 3 | Allen, Mr. William Henry | male | 35.0 | 0 | 0 | 373450 | 8.0500 | NaN | S |
In [10]:
df.iloc[0:5,1:3]#指定0-5行数据 1-3不包含第3列
Out[10]:
| Survived | Pclass | |
|---|---|---|
| 0 | 0 | 3 |
| 1 | 1 | 1 |
| 2 | 1 | 3 |
| 3 | 1 | 1 |
| 4 | 0 | 3 |
In [11]:
df=df.set_index('Name')
In [13]:
df.loc['Heikkinen, Miss. Laina']
Out[13]:
PassengerId 3 Survived 1 Pclass 3 Sex female Age 26 SibSp 0 Parch 0 Ticket STON/O2. 3101282 Fare 7.925 Cabin NaN Embarked S Name: Heikkinen, Miss. Laina, dtype: object
In [14]:
df.loc['Heikkinen, Miss. Laina','Fare']
Out[14]:
7.925
In [16]:
df.loc['Heikkinen, Miss. Laina':'Allen, Mr. William Henry',:]#以lable作为索引
Out[16]:
| PassengerId | Survived | Pclass | Sex | Age | SibSp | Parch | Ticket | Fare | Cabin | Embarked | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| Name | |||||||||||
| Heikkinen, Miss. Laina | 3 | 1 | 3 | female | 26.0 | 0 | 0 | STON/O2. 3101282 | 7.925 | NaN | S |
| Futrelle, Mrs. Jacques Heath (Lily May Peel) | 4 | 1 | 1 | female | 35.0 | 1 | 0 | 113803 | 53.100 | C123 | S |
| Allen, Mr. William Henry | 5 | 0 | 3 | male | 35.0 | 0 | 0 | 373450 | 8.050 | NaN | S |
In [17]:
df.loc['Heikkinen, Miss. Laina','Fare']=1000#具体定位后赋值操作
df.head()
Out[17]:
| PassengerId | Survived | Pclass | Sex | Age | SibSp | Parch | Ticket | Fare | Cabin | Embarked | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| Name | |||||||||||
| Braund, Mr. Owen Harris | 1 | 0 | 3 | male | 22.0 | 1 | 0 | A/5 21171 | 7.2500 | NaN | S |
| Cumings, Mrs. John Bradley (Florence Briggs Thayer) | 2 | 1 | 1 | female | 38.0 | 1 | 0 | PC 17599 | 71.2833 | C85 | C |
| Heikkinen, Miss. Laina | 3 | 1 | 3 | female | 26.0 | 0 | 0 | STON/O2. 3101282 | 1000.0000 | NaN | S |
| Futrelle, Mrs. Jacques Heath (Lily May Peel) | 4 | 1 | 1 | female | 35.0 | 1 | 0 | 113803 | 53.1000 | C123 | S |
| Allen, Mr. William Henry | 5 | 0 | 3 | male | 35.0 | 0 | 0 | 373450 | 8.0500 | NaN | S |
In [18]:
#被遗弃了,现在不用了
df.ix['Heikkinen, Miss. Laina','Fare']
E:\software\Anaconda3 5.2.0\lib\site-packages\ipykernel_launcher.py:2: DeprecationWarning: .ix is deprecated. Please use .loc for label based indexing or .iloc for positional indexing See the documentation here: http://pandas.pydata.org/pandas-docs/stable/indexing.html#ix-indexer-is-deprecated
Out[18]:
1000.0
bool类型的索引¶
In [19]:
df['Fare']>10#返回BOOL值
Out[19]:
Name
Braund, Mr. Owen Harris False
Cumings, Mrs. John Bradley (Florence Briggs Thayer) True
Heikkinen, Miss. Laina True
Futrelle, Mrs. Jacques Heath (Lily May Peel) True
Allen, Mr. William Henry False
Moran, Mr. James False
McCarthy, Mr. Timothy J True
Palsson, Master. Gosta Leonard True
Johnson, Mrs. Oscar W (Elisabeth Vilhelmina Berg) True
Nasser, Mrs. Nicholas (Adele Achem) True
Sandstrom, Miss. Marguerite Rut True
Bonnell, Miss. Elizabeth True
Saundercock, Mr. William Henry False
Andersson, Mr. Anders Johan True
Vestrom, Miss. Hulda Amanda Adolfina False
Hewlett, Mrs. (Mary D Kingcome) True
Rice, Master. Eugene True
Williams, Mr. Charles Eugene True
Vander Planke, Mrs. Julius (Emelia Maria Vandemoortele) True
Masselmani, Mrs. Fatima False
Fynney, Mr. Joseph J True
Beesley, Mr. Lawrence True
McGowan, Miss. Anna "Annie" False
Sloper, Mr. William Thompson True
Palsson, Miss. Torborg Danira True
Asplund, Mrs. Carl Oscar (Selma Augusta Emilia Johansson) True
Emir, Mr. Farred Chehab False
Fortune, Mr. Charles Alexander True
O'Dwyer, Miss. Ellen "Nellie" False
Todoroff, Mr. Lalio False
...
Giles, Mr. Frederick Edward True
Swift, Mrs. Frederick Joel (Margaret Welles Barron) True
Sage, Miss. Dorothy Edith "Dolly" True
Gill, Mr. John William True
Bystrom, Mrs. (Karolina) True
Duran y More, Miss. Asuncion True
Roebling, Mr. Washington Augustus II True
van Melkebeke, Mr. Philemon False
Johnson, Master. Harold Theodor True
Balkic, Mr. Cerin False
Beckwith, Mrs. Richard Leonard (Sallie Monypeny) True
Carlsson, Mr. Frans Olof False
Vander Cruyssen, Mr. Victor False
Abelson, Mrs. Samuel (Hannah Wizosky) True
Najib, Miss. Adele Kiamie "Jane" False
Gustafsson, Mr. Alfred Ossian False
Petroff, Mr. Nedelio False
Laleff, Mr. Kristo False
Potter, Mrs. Thomas Jr (Lily Alexenia Wilson) True
Shelley, Mrs. William (Imanita Parrish Hall) True
Markun, Mr. Johann False
Dahlberg, Miss. Gerda Ulrika True
Banfield, Mr. Frederick James True
Sutehall, Mr. Henry Jr False
Rice, Mrs. William (Margaret Norton) True
Montvila, Rev. Juozas True
Graham, Miss. Margaret Edith True
Johnston, Miss. Catherine Helen "Carrie" True
Behr, Mr. Karl Howell True
Dooley, Mr. Patrick False
Name: Fare, Length: 891, dtype: bool
In [20]:
df[df['Fare']>40]#把Bool类型传到 data_frame中=>显示大于40的表格
Out[20]:
| PassengerId | Survived | Pclass | Sex | Age | SibSp | Parch | Ticket | Fare | Cabin | Embarked | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| Name | |||||||||||
| Cumings, Mrs. John Bradley (Florence Briggs Thayer) | 2 | 1 | 1 | female | 38.0 | 1 | 0 | PC 17599 | 71.2833 | C85 | C |
| Heikkinen, Miss. Laina | 3 | 1 | 3 | female | 26.0 | 0 | 0 | STON/O2. 3101282 | 1000.0000 | NaN | S |
| Futrelle, Mrs. Jacques Heath (Lily May Peel) | 4 | 1 | 1 | female | 35.0 | 1 | 0 | 113803 | 53.1000 | C123 | S |
| McCarthy, Mr. Timothy J | 7 | 0 | 1 | male | 54.0 | 0 | 0 | 17463 | 51.8625 | E46 | S |
| Fortune, Mr. Charles Alexander | 28 | 0 | 1 | male | 19.0 | 3 | 2 | 19950 | 263.0000 | C23 C25 C27 | S |
| Spencer, Mrs. William Augustus (Marie Eugenie) | 32 | 1 | 1 | female | NaN | 1 | 0 | PC 17569 | 146.5208 | B78 | C |
| Meyer, Mr. Edgar Joseph | 35 | 0 | 1 | male | 28.0 | 1 | 0 | PC 17604 | 82.1708 | NaN | C |
| Holverson, Mr. Alexander Oskar | 36 | 0 | 1 | male | 42.0 | 1 | 0 | 113789 | 52.0000 | NaN | S |
| Laroche, Miss. Simonne Marie Anne Andree | 44 | 1 | 2 | female | 3.0 | 1 | 2 | SC/Paris 2123 | 41.5792 | NaN | C |
| Harper, Mrs. Henry Sleeper (Myna Haxtun) | 53 | 1 | 1 | female | 49.0 | 1 | 0 | PC 17572 | 76.7292 | D33 | C |
| Ostby, Mr. Engelhart Cornelius | 55 | 0 | 1 | male | 65.0 | 0 | 1 | 113509 | 61.9792 | B30 | C |
| Goodwin, Master. William Frederick | 60 | 0 | 3 | male | 11.0 | 5 | 2 | CA 2144 | 46.9000 | NaN | S |
| Icard, Miss. Amelie | 62 | 1 | 1 | female | 38.0 | 0 | 0 | 113572 | 80.0000 | B28 | NaN |
| Harris, Mr. Henry Birkhardt | 63 | 0 | 1 | male | 45.0 | 1 | 0 | 36973 | 83.4750 | C83 | S |
| Goodwin, Miss. Lillian Amy | 72 | 0 | 3 | female | 16.0 | 5 | 2 | CA 2144 | 46.9000 | NaN | S |
| Hood, Mr. Ambrose Jr | 73 | 0 | 2 | male | 21.0 | 0 | 0 | S.O.C. 14879 | 73.5000 | NaN | S |
| Bing, Mr. Lee | 75 | 1 | 3 | male | 32.0 | 0 | 0 | 1601 | 56.4958 | NaN | S |
| Carrau, Mr. Francisco M | 84 | 0 | 1 | male | 28.0 | 0 | 0 | 113059 | 47.1000 | NaN | S |
| Fortune, Miss. Mabel Helen | 89 | 1 | 1 | female | 23.0 | 3 | 2 | 19950 | 263.0000 | C23 C25 C27 | S |
| Chaffee, Mr. Herbert Fuller | 93 | 0 | 1 | male | 46.0 | 1 | 0 | W.E.P. 5734 | 61.1750 | E31 | S |
| Greenfield, Mr. William Bertram | 98 | 1 | 1 | male | 23.0 | 0 | 1 | PC 17759 | 63.3583 | D10 D12 | C |
| White, Mr. Richard Frasar | 103 | 0 | 1 | male | 21.0 | 0 | 1 | 35281 | 77.2875 | D26 | S |
| Porter, Mr. Walter Chamberlain | 111 | 0 | 1 | male | 47.0 | 0 | 0 | 110465 | 52.0000 | C110 | S |
| Baxter, Mr. Quigg Edmond | 119 | 0 | 1 | male | 24.0 | 0 | 1 | PC 17558 | 247.5208 | B58 B60 | C |
| Hickman, Mr. Stanley George | 121 | 0 | 2 | male | 21.0 | 2 | 0 | S.O.C. 14879 | 73.5000 | NaN | S |
| White, Mr. Percival Wayland | 125 | 0 | 1 | male | 54.0 | 0 | 1 | 35281 | 77.2875 | D26 | S |
| Futrelle, Mr. Jacques Heath | 138 | 0 | 1 | male | 37.0 | 1 | 0 | 113803 | 53.1000 | C123 | S |
| Giglio, Mr. Victor | 140 | 0 | 1 | male | 24.0 | 0 | 0 | PC 17593 | 79.2000 | B86 | C |
| Pears, Mrs. Thomas (Edith Wearne) | 152 | 1 | 1 | female | 22.0 | 1 | 0 | 113776 | 66.6000 | C2 | S |
| Williams, Mr. Charles Duane | 156 | 0 | 1 | male | 51.0 | 0 | 1 | PC 17597 | 61.3792 | NaN | C |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| Endres, Miss. Caroline Louise | 717 | 1 | 1 | female | 38.0 | 0 | 0 | PC 17757 | 227.5250 | C45 | C |
| Chambers, Mr. Norman Campbell | 725 | 1 | 1 | male | 27.0 | 1 | 0 | 113806 | 53.1000 | E8 | S |
| Allen, Miss. Elisabeth Walton | 731 | 1 | 1 | female | 29.0 | 0 | 0 | 24160 | 211.3375 | B5 | S |
| Lesurer, Mr. Gustave J | 738 | 1 | 1 | male | 35.0 | 0 | 0 | PC 17755 | 512.3292 | B101 | C |
| Cavendish, Mr. Tyrell William | 742 | 0 | 1 | male | 36.0 | 1 | 0 | 19877 | 78.8500 | C46 | S |
| Ryerson, Miss. Susan Parker "Suzette" | 743 | 1 | 1 | female | 21.0 | 2 | 2 | PC 17608 | 262.3750 | B57 B59 B63 B66 | C |
| Crosby, Capt. Edward Gifford | 746 | 0 | 1 | male | 70.0 | 1 | 1 | WE/P 5735 | 71.0000 | B22 | S |
| Marvin, Mr. Daniel Warner | 749 | 0 | 1 | male | 19.0 | 1 | 0 | 113773 | 53.1000 | D30 | S |
| Herman, Mrs. Samuel (Jane Laver) | 755 | 1 | 2 | female | 48.0 | 1 | 2 | 220845 | 65.0000 | NaN | S |
| Rothes, the Countess. of (Lucy Noel Martha Dyer-Edwards) | 760 | 1 | 1 | female | 33.0 | 0 | 0 | 110152 | 86.5000 | B77 | S |
| Carter, Mrs. William Ernest (Lucile Polk) | 764 | 1 | 1 | female | 36.0 | 1 | 2 | 113760 | 120.0000 | B96 B98 | S |
| Hogeboom, Mrs. John C (Anna Andrews) | 766 | 1 | 1 | female | 51.0 | 1 | 0 | 13502 | 77.9583 | D11 | S |
| Robert, Mrs. Edward Scott (Elisabeth Walton McMillan) | 780 | 1 | 1 | female | 43.0 | 0 | 1 | 24160 | 211.3375 | B3 | S |
| Dick, Mrs. Albert Adrian (Vera Gillespie) | 782 | 1 | 1 | female | 17.0 | 1 | 0 | 17474 | 57.0000 | B20 | S |
| Guggenheim, Mr. Benjamin | 790 | 0 | 1 | male | 46.0 | 0 | 0 | PC 17593 | 79.2000 | B82 B84 | C |
| Sage, Miss. Stella Anna | 793 | 0 | 3 | female | NaN | 8 | 2 | CA. 2343 | 69.5500 | NaN | S |
| Carter, Master. William Thornton II | 803 | 1 | 1 | male | 11.0 | 1 | 2 | 113760 | 120.0000 | B96 B98 | S |
| Chambers, Mrs. Norman Campbell (Bertha Griggs) | 810 | 1 | 1 | female | 33.0 | 1 | 0 | 113806 | 53.1000 | E8 | S |
| Hays, Mrs. Charles Melville (Clara Jennings Gregg) | 821 | 1 | 1 | female | 52.0 | 1 | 1 | 12749 | 93.5000 | B69 | S |
| Lam, Mr. Len | 827 | 0 | 3 | male | NaN | 0 | 0 | 1601 | 56.4958 | NaN | S |
| Stone, Mrs. George Nelson (Martha Evelyn) | 830 | 1 | 1 | female | 62.0 | 0 | 0 | 113572 | 80.0000 | B28 | NaN |
| Compton, Miss. Sara Rebecca | 836 | 1 | 1 | female | 39.0 | 1 | 1 | PC 17756 | 83.1583 | E49 | C |
| Chip, Mr. Chang | 839 | 1 | 3 | male | 32.0 | 0 | 0 | 1601 | 56.4958 | NaN | S |
| Sage, Mr. Douglas Bullen | 847 | 0 | 3 | male | NaN | 8 | 2 | CA. 2343 | 69.5500 | NaN | S |
| Goldenberg, Mrs. Samuel L (Edwiga Grabowska) | 850 | 1 | 1 | female | NaN | 1 | 0 | 17453 | 89.1042 | C92 | C |
| Wick, Mrs. George Dennick (Mary Hitchcock) | 857 | 1 | 1 | female | 45.0 | 1 | 1 | 36928 | 164.8667 | NaN | S |
| Sage, Miss. Dorothy Edith "Dolly" | 864 | 0 | 3 | female | NaN | 8 | 2 | CA. 2343 | 69.5500 | NaN | S |
| Roebling, Mr. Washington Augustus II | 868 | 0 | 1 | male | 31.0 | 0 | 0 | PC 17590 | 50.4958 | A24 | S |
| Beckwith, Mrs. Richard Leonard (Sallie Monypeny) | 872 | 1 | 1 | female | 47.0 | 1 | 1 | 11751 | 52.5542 | D35 | S |
| Potter, Mrs. Thomas Jr (Lily Alexenia Wilson) | 880 | 1 | 1 | female | 56.0 | 0 | 1 | 11767 | 83.1583 | C50 | C |
177 rows × 11 columns
In [21]:
df[df['Sex']=='male'][:5]
Out[21]:
| PassengerId | Survived | Pclass | Sex | Age | SibSp | Parch | Ticket | Fare | Cabin | Embarked | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| Name | |||||||||||
| Braund, Mr. Owen Harris | 1 | 0 | 3 | male | 22.0 | 1 | 0 | A/5 21171 | 7.2500 | NaN | S |
| Allen, Mr. William Henry | 5 | 0 | 3 | male | 35.0 | 0 | 0 | 373450 | 8.0500 | NaN | S |
| Moran, Mr. James | 6 | 0 | 3 | male | NaN | 0 | 0 | 330877 | 8.4583 | NaN | Q |
| McCarthy, Mr. Timothy J | 7 | 0 | 1 | male | 54.0 | 0 | 0 | 17463 | 51.8625 | E46 | S |
| Palsson, Master. Gosta Leonard | 8 | 0 | 3 | male | 2.0 | 3 | 1 | 349909 | 21.0750 | NaN | S |
In [22]:
df.loc[df['Sex']=='male','Age'].mean()#索引性别,然后对年龄求平均值
Out[22]:
30.72664459161148
In [24]:
(df['Age']>70).sum()#对大于70年龄的人统计个数
Out[24]:
5
此资源来自https://www.cnblogs.com/AI-robort/,博客园的karina512。

浙公网安备 33010602011771号