w3resource

Pandas Series: str.get() function

Series-str.get() function

The str.get() function is used to extract element from each component at specified position.

Extract element from lists, tuples, or strings in each element in the Series/Index.

Syntax:

Series.str.get(self, i)
Pandas Series: str.get() function

Parameters:

Name Description Type/Default Value Required / Optional
i Position of element to extract. int Required

Returns: Series or Index

Example:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(["String",
              (2, 3, 4),
              ["p", "q", "r"],
              234,
              -567,
              {1: "Hiii", "2": "friends"}])
s

Output:

0                         String
1                      (2, 3, 4)
2                      [p, q, r]
3                            234
4                           -567
5    {1: 'Hiii', '2': 'friends'}
dtype: object

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(["String",
              (2, 3, 4),
              ["p", "q", "r"],
              234,
              -567,
              {1: "Hiii", "2": "friends"}])
s.str.get(1)

Output:

0       t
1       3
2       q
3     NaN
4     NaN
5    Hiii
dtype: object

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(["String",
              (2, 3, 4),
              ["p", "q", "r"],
              234,
              -567,
              {1: "Hiii", "2": "friends"}])
s.str.get(-1)

Output:

0       g
1       4
2       r
3     NaN
4     NaN
5    None
dtype: object
Pandas Series: str.get() function

Previous: Series-str.findall() function
Next: Series-str.join() function



Follow us on Facebook and Twitter for latest update.