Get list from index to index c#

< Introducing Pandas Objects | Contents | Operating on Data in Pandas >

In Chapter 2, we looked in detail at methods and tools to access, set, and modify values in NumPy arrays. These included indexing [e.g., arr[2, 1]], slicing [e.g., arr[:, 1:5]], masking [e.g., arr[arr > 0]], fancy indexing [e.g., arr[0, [1, 5]]], and combinations thereof [e.g., arr[:, [1, 5]]]. Here we'll look at similar means of accessing and modifying values in Pandas Series and DataFrame objects. If you have used the NumPy patterns, the corresponding patterns in Pandas will feel very familiar, though there are a few quirks to be aware of.

We'll start with the simple case of the one-dimensional Series object, and then move on to the more complicated two-dimesnional DataFrame object.

Data Selection in Series¶

As we saw in the previous section, a Series object acts in many ways like a one-dimensional NumPy array, and in many ways like a standard Python dictionary. If we keep these two overlapping analogies in mind, it will help us to understand the patterns of data indexing and selection in these arrays.

Series as dictionary¶

Like a dictionary, the Series object provides a mapping from a collection of keys to a collection of values:

In[1]:
import pandas as pd data = pd.Series[[0.25, 0.5, 0.75, 1.0], index=['a', 'b', 'c', 'd']] data
Out[1]:
a 0.25 b 0.50 c 0.75 d 1.00 dtype: float64
In[2]:
data['b']
Out[2]:
0.5

We can also use dictionary-like Python expressions and methods to examine the keys/indices and values:

In[3]:
'a' in data
Out[3]:
True
In[4]:
data.keys[]
Out[4]:
Index[['a', 'b', 'c', 'd'], dtype='object']
In[5]:
list[data.items[]]
Out[5]:
[['a', 0.25], ['b', 0.5], ['c', 0.75], ['d', 1.0]]

Series objects can even be modified with a dictionary-like syntax. Just as you can extend a dictionary by assigning to a new key, you can extend a Series by assigning to a new index value:

In[6]:
data['e'] = 1.25 data
Out[6]:
a 0.25 b 0.50 c 0.75 d 1.00 e 1.25 dtype: float64

This easy mutability of the objects is a convenient feature: under the hood, Pandas is making decisions about memory layout and data copying that might need to take place; the user generally does not need to worry about these issues.

Series as one-dimensional array¶

A Series builds on this dictionary-like interface and provides array-style item selection via the same basic mechanisms as NumPy arrays that is, slices, masking, and fancy indexing. Examples of these are as follows:

In[7]:
# slicing by explicit index data['a':'c']
Out[7]:
a 0.25 b 0.50 c 0.75 dtype: float64
In[8]:
# slicing by implicit integer index data[0:2]
Out[8]:
a 0.25 b 0.50 dtype: float64
In[9]:
# masking data[[data > 0.3] & [data 100, ['pop', 'density']]
Out[31]:
popdensityFloridaNew York
19552860114.806121
19651127139.076746

Any of these indexing conventions may also be used to set or modify values; this is done in the standard way that you might be accustomed to from working with NumPy:

In[32]:
data.iloc[0, 2] = 90 data
Out[32]:
areapopdensityCaliforniaFloridaIllinoisNew YorkTexas
4239673833252190.000000
17031219552860114.806121
1499951288213585.883763
14129719651127139.076746
6956622644819338.018740

To build up your fluency in Pandas data manipulation, I suggest spending some time with a simple DataFrame and exploring the types of indexing, slicing, masking, and fancy indexing that are allowed by these various indexing approaches.

Additional indexing conventions¶

There are a couple extra indexing conventions that might seem at odds with the preceding discussion, but nevertheless can be very useful in practice. First, while indexing refers to columns, slicing refers to rows:

In[33]:
data['Florida':'Illinois']
Out[33]:
areapopdensityFloridaIllinois
17031219552860114.806121
1499951288213585.883763

Such slices can also refer to rows by number rather than by index:

In[34]:
data[1:3]
Out[34]:
areapopdensityFloridaIllinois
17031219552860114.806121
1499951288213585.883763

Similarly, direct masking operations are also interpreted row-wise rather than column-wise:

In[35]:
data[data.density > 100]
Out[35]:
areapopdensityFloridaNew York
17031219552860114.806121
14129719651127139.076746

These two conventions are syntactically similar to those on a NumPy array, and while these may not precisely fit the mold of the Pandas conventions, they are nevertheless quite useful in practice.

< Introducing Pandas Objects | Contents | Operating on Data in Pandas >

Video liên quan

Chủ Đề