官术网_书友最值得收藏!

Indexing and selecting data

In this section, we will focus on how to get, set, or slice subsets of Pandas data structure objects. As we learned in previous sections, Series or DataFrame objects have axis labeling information. This information can be used to identify items that we want to select or assign a new value to in the object:

>>> s4[['024', '002']] # selecting data of Series object
024 NaN
002 Mary
dtype: object
>>> s4[['024', '002']] = 'unknown' # assigning data
>>> s4
024 unknown
065 NaN
002 unknown
001 Nam
dtype: object

If the data object is a DataFrame structure, we can also proceed in a similar way:

>>> df5[['b', 'c']]
 b c
0 1 2
1 4 5
2 7 8

For label indexing on the rows of DataFrame, we use the ix function that enables us to select a set of rows and columns in the object. There are two parameters that we need to specify: the row and column labels that we want to get. By default, if we do not specify the selected column names, the function will return selected rows with all columns in the object:

>>> df5.ix[0]
a 0
b 1
c 2
Name: 0, dtype: int64
>>> df5.ix[0, 1:3]
b 1
c 2
Name: 0, dtype: int64

Moreover, we have many ways to select and edit data contained in a Pandas object. We summarize these functions in the following table:

Tip

Pandas data objects may contain duplicate indices. In this case, when we get or set a data value via index label, it will affect all rows or columns that have the same selected index name.

主站蜘蛛池模板: 扎赉特旗| 阿拉善左旗| 赤城县| 金乡县| 汾西县| 巴马| 深水埗区| 宁武县| 湖州市| 襄樊市| 德清县| 安平县| 临洮县| 慈利县| 泉州市| 榆社县| 陆丰市| 宁武县| 洪雅县| 丹棱县| 孟村| 长汀县| 花莲县| 鹤山市| 台中县| 屯昌县| 黄浦区| 宁南县| 余江县| 遵义县| 澎湖县| 平乐县| 澎湖县| 长沙县| 安溪县| 南漳县| 陆川县| 赤壁市| 连平县| 茶陵县| 绵阳市|