Pandas DataFrame: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 21: Line 21:
=Accessing Elements of a DataFrame=
=Accessing Elements of a DataFrame=
==<tt>iloc[]</tt>==
==<tt>iloc[]</tt>==
A property that allows integer-based access (indexing). The location is specified as a 0-based index position. The property accepts a wide variety of arguments. Some of the most common are:
A property that allows integer-based access (indexing). The location is specified as a 0-based index position. The property accepts a wide variety of arguments.


<code>iloc[<row-index-slice>, <column-index>]</code>
Used in the following situations:
===Extract a Series from the DataFrame===
<syntaxhighlight lang='py'>
df = ...
# extract a series corresponding to DataFrame column 0
s = df.iloc[:,0]
</syntaxhighlight>


==<tt>loc[]</tt>==
==<tt>loc[]</tt>==

Revision as of 18:41, 8 October 2023

External

Internal

Overview

A DataFrame is a two-dimensional data structure with columns of potentially different types. The data structure also contains labeled axes, for both rows and columns.

Can be thought of as a dict-like container for Series objects, where each column is a Series. The dimensionality of the DataFrame is given by its shape property.

Shape

shape is a property of the DataFrame, containing a tuple that returns the dimensionality of the DataFrame: rows, columns.

Create a DataFrame

Create a DataFrame from a CSV File

Accessing Elements of a DataFrame

iloc[]

A property that allows integer-based access (indexing). The location is specified as a 0-based index position. The property accepts a wide variety of arguments.

Used in the following situations:

Extract a Series from the DataFrame

df = ...
# extract a series corresponding to DataFrame column 0
s = df.iloc[:,0]

loc[]

A property that allows label-based access (indexing).

squeeze()

[]

Operations on DataFrames