str.len()

Length of the string data.
Returns Series or Index of Int

Length of the string column ( NAME )
import pandas as pd 
my_dict={'NAME':['Ravi','Raju','Alex','Ron','King','Jack'],
         'ID':[1,2,3,4,5,6],
         'MATH':[80,40,70,70,70,30],
         'ENGLISH':[80,70,40,50,60,30]}
my_data = pd.DataFrame(data=my_dict)
print(my_data['NAME'].str.len())
Output
0    4
1    4
2    4
3    3
4    4
5    4
Adding one column showing length of the string field ( NAME )
import pandas as pd 
my_dict={'NAME':['Ravi','Raju','Alex','Ron','King','Jack'],
         'ID':[1,2,3,4,5,6],
         'MATH':[80,40,70,70,70,30],
         'ENGLISH':[80,70,40,50,60,30]}
my_data = pd.DataFrame(data=my_dict)
my_data['LENGTH']=my_data['NAME'].str.len()
print(my_data )
Output
   NAME  ID  MATH  ENGLISH  LENGTH
0  Ravi   1    80       80       4
1  Raju   2    40       70       4
2  Alex   3    70       40       4
3   Ron   4    70       50       3
4  King   5    70       60       4
5  Jack   6    30       30       4

Length of Integer column

We can get the length of one integer column ( Say ENGLISH ) by using len() , but we have to convert the datatype from integer to string by using astype() before using len().
import pandas as pd 
my_dict={'NAME':['Ravi','Raju','Alex','Ron','King','Jack'],
         'ID':[1,2,3,4,5,6],
         'MATH':[80,40,70,70,70,30],
         'ENGLISH':[80,70,40,50,60,30]}
my_data = pd.DataFrame(data=my_dict)
my_data['LENGTH']=my_data['ENGLISH'].astype(str).str.len()
print(my_data )
Output
   NAME  ID  MATH  ENGLISH  LENGTH
0  Ravi   1    80       80       2
1  Raju   2    40       70       2
2  Alex   3    70       40       2
3   Ron   4    70       50       2
4  King   5    70       60       2
5  Jack   6    30       30       2
Let us use one float datatype and find out the length. Here also we will use astype() to convert float to string data. Here we changed MATH column by adding float data.
import pandas as pd 
my_dict={'NAME':['Ravi','Raju','Alex','Ron','King','Jack'],
         'ID':[1,2,3,4,5,6],
         'MATH':[80.34,40.21,70.456,70.123,70.0,30.9],
         'ENGLISH':[80,70,40,50,60,30]}
my_data = pd.DataFrame(data=my_dict)
my_data['LENGTH']=my_data['MATH'].astype(str).str.len()
print(my_data)
Output
   NAME  ID    MATH  ENGLISH  LENGTH
0  Ravi   1  80.340       80       5
1  Raju   2  40.210       70       5
2  Alex   3  70.456       40       6
3   Ron   4  70.123       50       6
4  King   5  70.000       60       4
5  Jack   6  30.900       30       4
Pandas contains() Converting char case slice() split()
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer