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.
str.len() method is used to determine length of each string in a Pandas series. This method is only for series of strings.
Since this is a string method, .str has to be prefixed everytime before calling this method. Otherwise it will give an error.
Return type: Series of integer values. NULL values might be present too depending upon caller series.
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: Calculating length of string series (dtype=str)
In this example, the string length of Name column is calculated using str.len() method. The dtype of the Series is already string. So there is no need of data type conversion. Before doing any operations, null rows are removed to avoid errors.
As shown in the output image, the length of each string in name column is returned.
- This method doesn’t count length of integer or float series. It will give an error since it’s not a string series. The series need to be converted first ( Shown in next Example)
- There is no parameter to handle null values. A null value would return null in the output string too.
In this example, the length of salary column is calculated using the str.len() method. Since the series is imported as float64 dtype, it’s first converted to string using .astype() method.
As shown in the output, length of int or float series can only be computed by converting it to string dtype.
- Python | pandas.map()
- Python | Pandas Series.add()
- Python | Pandas dataframe.sub()
- Python | Pandas.pivot_table()
- Python | Pandas TimedeltaIndex.name
- Python | Pandas Panel.pow()
- Python | Pandas Panel.div()
- Python | Pandas dataframe.mad()
- Python | Pandas Series.gt()
- Python | Pandas Series.all()
- Python | Pandas Series.sem()
- Python | Pandas Series.between()
- Introduction to Pandas in Python
- Python | Pandas Series.sub()
- Python | Pandas.factorize()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.