To select a single column, use square brackets [] with the column
name of the column of interest.
Each column in a DataFrame is a Series. As a single column is
selected, the returned object is a pandas Series. We can verify this
by checking the type of the output:
In [6]: type(titanic["Age"])Out[6]: pandas.core.series.Series
And have a look at the shape of the output:
In [7]: titanic["Age"].shapeOut[7]: (891,)
DataFrame.shape is an attribute (remember tutorial on reading and writing, do not use parentheses for attributes) of a
pandas Series and DataFrame containing the number of rows and
columns: (nrows, ncolumns). A pandas Series is 1-dimensional and only
the number of rows is returned.
I’m interested in the age and sex of the Titanic passengers.
In [8]: age_sex=titanic[["Age","Sex"]]In [9]: age_sex.head()Out[9]: Age Sex0 22.0 male1 38.0 female2 26.0 female3 35.0 female4 35.0 male
To select multiple columns, use a list of column names within the
selection brackets [].
The returned data type is a pandas DataFrame:
In [10]: type(titanic[["Age","Sex"]])Out[10]: pandas.core.frame.DataFrame
In [11]: titanic[["Age","Sex"]].shapeOut[11]: (891, 2)
The selection returned a DataFrame with 891 rows and 2 columns. Remember, a
DataFrame is 2-dimensional with both a row and column dimension.
The output of the conditional expression (>, but also ==,
!=, <, <=,… would work) is actually a pandas Series of
boolean values (either True or False) with the same number of
rows as the original DataFrame. Such a Series of boolean values
can be used to filter the DataFrame by putting it in between the
selection brackets []. Only rows for which the value is True
will be selected.
We know from before that the original Titanic DataFrame consists of
891 rows. Let’s have a look at the number of rows which satisfy the
condition by checking the shape attribute of the resulting
DataFrameabove_35:
In [15]: above_35.shapeOut[15]: (217, 12)
I’m interested in the Titanic passengers from cabin class 2 and 3.
In [16]: class_23=titanic[titanic["Pclass"].isin([2,3])]In [17]: class_23.head()Out[17]: PassengerId Survived Pclass ... Fare Cabin Embarked0 1 0 3 ... 7.2500 NaN S2 3 1 3 ... 7.9250 NaN S4 5 0 3 ... 8.0500 NaN S5 6 0 3 ... 8.4583 NaN Q7 8 0 3 ... 21.0750 NaN S[5 rows x 12 columns]
Similar to the conditional expression, the isin() conditional function
returns a True for each row the values are in the provided list. To
filter the rows based on such a function, use the conditional function
inside the selection brackets []. In this case, the condition inside
the selection brackets titanic["Pclass"].isin([2,3]) checks for
which rows the Pclass column is either 2 or 3.
The above is equivalent to filtering by rows for which the class is
either 2 or 3 and combining the two statements with an | (or)
operator:
In [18]: class_23=titanic[(titanic["Pclass"]==2)|(titanic["Pclass"]==3)]In [19]: class_23.head()Out[19]: PassengerId Survived Pclass ... Fare Cabin Embarked0 1 0 3 ... 7.2500 NaN S2 3 1 3 ... 7.9250 NaN S4 5 0 3 ... 8.0500 NaN S5 6 0 3 ... 8.4583 NaN Q7 8 0 3 ... 21.0750 NaN S[5 rows x 12 columns]
I want to work with passenger data for which the age is known.
In [20]: age_no_na=titanic[titanic["Age"].notna()]In [21]: age_no_na.head()Out[21]: PassengerId Survived Pclass ... Fare Cabin Embarked0 1 0 3 ... 7.2500 NaN S1 2 1 1 ... 71.2833 C85 C2 3 1 3 ... 7.9250 NaN S3 4 1 1 ... 53.1000 C123 S4 5 0 3 ... 8.0500 NaN S[5 rows x 12 columns]
The notna() conditional function returns a True for each row the
values are not a Null value. As such, this can be combined with the
selection brackets [] to filter the data table.
You might wonder what actually changed, as the first 5 lines are still
the same values. One way to verify is to check if the shape has changed:
In [22]: age_no_na.shapeOut[22]: (714, 12)
To user guide
For more dedicated functions on missing values, see the user guide section about handling missing data.
1 Cumings, Mrs. John Bradley (Florence Briggs Th...
6 McCarthy, Mr. Timothy J
11 Bonnell, Miss. Elizabeth
13 Andersson, Mr. Anders Johan
15 Hewlett, Mrs. (Mary D Kingcome)
Name: Name, dtype: object
In this case, a subset of both rows and columns is made in one go and
just using selection brackets [] is not sufficient anymore. The
loc/iloc operators are required in front of the selection
brackets []. When using loc/iloc, the part before the comma
is the rows you want, and the part after the comma is the columns you
want to select.
When using the column names, row labels or a condition expression, use
the loc operator in front of the selection brackets []. For both
the part before and after the comma, you can use a single label, a list
of labels, a slice of labels, a conditional expression or a colon. Using
a colon specifies you want to select all rows or columns.
I’m interested in rows 10 till 25 and columns 3 to 5.
In [25]: titanic.iloc[9:25,2:5]Out[25]: Pclass Name Sex9 2 Nasser, Mrs. Nicholas (Adele Achem) female10 3 Sandstrom, Miss. Marguerite Rut female11 1 Bonnell, Miss. Elizabeth female12 3 Saundercock, Mr. William Henry male13 3 Andersson, Mr. Anders Johan male.. ... ... ...20 2 Fynney, Mr. Joseph J male21 2 Beesley, Mr. Lawrence male22 3 McGowan, Miss. Anna "Annie" female23 1 Sloper, Mr. William Thompson male24 3 Palsson, Miss. Torborg Danira female[16 rows x 3 columns]
Again, a subset of both rows and columns is made in one go and just
using selection brackets [] is not sufficient anymore. When
specifically interested in certain rows and/or columns based on their
position in the table, use the iloc operator in front of the
selection brackets [].
When selecting specific rows and/or columns with loc or iloc,
new values can be assigned to the selected data. For example, to assign
the name anonymous to the first 3 elements of the fourth column:
In [26]: titanic.iloc[0:3,3]="anonymous"In [27]: titanic.head()Out[27]: PassengerId Survived Pclass ... Fare Cabin Embarked0 1 0 3 ... 7.2500 NaN S1 2 1 1 ... 71.2833 C85 C2 3 1 3 ... 7.9250 NaN S3 4 1 1 ... 53.1000 C123 S4 5 0 3 ... 8.0500 NaN S[5 rows x 12 columns]