Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
Pandas str.get() method is used to get element at the passed position. This method works for string, numeric values and even lists throughout the series. .str has to be prefixed every time to differentiate it from Python’s default get() method.
i : Position of element to be extracted, Integer values only.
Return type: Series with element/character at passed position
To download the CSV used in code, click here.
In the following examples, the data frame used contains data of some NBA players. The image of data frame before any operations is attached below.
Example #1: Getting character from string value
In this example, str.get() method is used to get a single character from the Name column. The null values have been removed using dropna() method and the series is converted to string type series using
.astype() before applying this method. This method can be used to get one character instead of whole string. For example, getting M from Male and F from Female since there can be two inputs only, so doing this can save data.
As shown in the output image, the New column is having first letter of the string in Name column.
Example #2: Getting elements from series of List
In this example, the Team column has been split at every occurrence of ” ” (Whitespace), into a list using str.split() method. Then the same column is overwritten with it. After that str.get() method is used to get elements in list at passed index.
As shown in the output images, The first image is of Elements at 0th position in series and the second image is of elements at 1st position in the series.
- Python | pandas.map()
- Python | Pandas TimedeltaIndex.take()
- Python | Pandas Series.ptp()
- Python | Pandas TimedeltaIndex.name
- Python | Pandas dataframe.eq()
- Python | Pandas dataframe.std()
- Python | Pandas Series.pop()
- Python | Pandas Period.day
- Python | Pandas Series.dt.day
- Python | Pandas Series.mul()
- Python | Pandas Period.second
- Python | Pandas Dataframe.pop()
- Python | Pandas Series.add()
- Python | Pandas Series.sub()
- Python | Pandas Series.max()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : nidhi_biet