How it works...

The col = df['item'] line of code selects the column index from the DataFrame and returns in the col variable a Series object containing the specified column. Analogously, the cols = df[['item', 'inventory']] line selects two columns. Notice that, in this case, the object returned is a DataFrame.

In the next example, we use rows = df[2:4] to select a range of rows from the DataFrame. Notice that this uses the standard convention for ranges in Python and rows two and three are returned.

The next two examples show the use of the loc indexing method.

With the row_data = df.loc[201024] line we select one row of data. The returned object is a Series, where the indexes are the column names of the DataFrame.

Then, the df1 = df.loc[201024:,['item','unit_price']] line is used to select a sub-array of the data. The first position specifies the 201024: slice, which represents a range starting at the 201024 index and extending to the end of the array. The second position is a list of column names, ['item','unit_price']. The returned DataFrame consists in the data corresponding to the given range of rows, restricted to the specified columns.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset