In [2]: titanic=pd.read_csv("data/titanic.csv")In [3]: titanic.head()Out[3]: 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]
Make all name characters lowercase.
In [4]: titanic["Name"].str.lower()Out[4]: 0 braund, mr. owen harris1 cumings, mrs. john bradley (florence briggs th...2 heikkinen, miss. laina3 futrelle, mrs. jacques heath (lily may peel)4 allen, mr. william henry ... 886 montvila, rev. juozas887 graham, miss. margaret edith888 johnston, miss. catherine helen "carrie"889 behr, mr. karl howell890 dooley, mr. patrickName: Name, Length: 891, dtype: object
To make each of the strings in the Name column lowercase, select the Name column
(see the tutorial on selection of data), add the str accessor and
apply the lower method. As such, each of the strings is converted element-wise.
Similar to datetime objects in the time series tutorial
having a dt accessor, a number of
specialized string methods are available when using the str
accessor. These methods have in general matching names with the
equivalent built-in string methods for single elements, but are applied
element-wise (remember element-wise calculations?)
on each of the values of the columns.
Create a new column Surname that contains the surname of the passengers by extracting the part before the comma.
In [5]: titanic["Name"].str.split(",")Out[5]: 0 [Braund, Mr. Owen Harris]1 [Cumings, Mrs. John Bradley (Florence Briggs ...2 [Heikkinen, Miss. Laina]3 [Futrelle, Mrs. Jacques Heath (Lily May Peel)]4 [Allen, Mr. William Henry] ... 886 [Montvila, Rev. Juozas]887 [Graham, Miss. Margaret Edith]888 [Johnston, Miss. Catherine Helen "Carrie"]889 [Behr, Mr. Karl Howell]890 [Dooley, Mr. Patrick]
To get the longest name we first have to get the lengths of each of the
names in the Name column. By using pandas string methods, the
function is applied to each of the names individually
(element-wise).
In the “Sex” column, replace values of “male” by “M” and values of “female” by “F”.
In [13]: titanic["Sex_short"]=titanic["Sex"].replace({"male":"M","female":"F"})In [14]: titanic["Sex_short"]Out[14]: 0 M1 F2 F3 F4 M ..
REMEMBER
String methods are available using the str accessor.
String methods work element-wise and can be used for conditional
indexing.
The replace method is a convenient method to convert values
according to a given dictionary.
Using the Series.str.split() method, each of the values is returned as a list of
2 elements. The first element is the part before the comma and the
second element is the part after the comma.
As we are only interested in the first part representing the surname
(element 0), we can again use the str accessor and apply Series.str.get() to
extract the relevant part. Indeed, these string functions can be
concatenated to combine multiple functions at once!
In [9]: titanic[titanic["Name"].str.contains("Countess")]Out[9]: PassengerId Survived Pclass ... Cabin Embarked Surname759 760 1 1 ... B77 S Rothes[1 rows x 13 columns]
The string method Series.str.contains() checks for each of the values in the
column Name if the string contains the word Countess and returns
for each of the values True (Countess is part of the name) or
False (Countess is not part of the name). This output can be used
to subselect the data using conditional (boolean) indexing introduced in
the subsetting of data tutorial. As there was
only one countess on the Titanic, we get one row as a result.
Series.str.len()
In [11]: titanic["Name"].str.len().idxmax()Out[11]: 307
Next, we need to get the corresponding location, preferably the index
label, in the table for which the name length is the largest. The
idxmax() method does exactly that. It is not a string method and is
applied to integers, so no str is used.
In [12]: titanic.loc[titanic["Name"].str.len().idxmax(),"Name"]Out[12]: 'Penasco y Castellana, Mrs. Victor de Satode (Maria Josefa Perez de Soto y Vallejo)'
Based on the index name of the row (307) and the column (Name),
we can do a selection using the loc operator, introduced in the
tutorial on subsetting.
886 M
887 F
888 F
889 M
890 M
Name: Sex_short, Length: 891, dtype: object
Whereas replace() is not a string method, it provides a convenient way
to use mappings or vocabularies to translate certain values. It requires
a dictionary to define the mapping {from:to}.
"Sex_short"
]
=
titanic
[
"Sex_short"
]
.
str
.
replace
(
"male"
,
"M"
)
This would become cumbersome and easily lead to mistakes. Just think (or
try out yourself) what would happen if those two statements are applied
in the opposite order…