How to find the maximum number of words and characters in sentences from a dataframe?

Viewed 2022

I'm loading user's input file that contains sentences into a data frame to display the maximum length of a sentence in characters and in words, but my code returns the lengths of each sentence in the dataframe. I just want the maximum values to be displayed. Any ideas where is my mistake?

res = wdata['sentences'].str.split().str.len()
print ("The maximum length in words are : " +  str(res)) 
length = wdata['sentences'].str.len().sort_values()
print ("The maximum length in chars are : " +  str(length)) 

#I expect the output to be 
#The maximum length in words are : 4
#The maximum length in words are : 40

enter image description here

1 Answers

Use max for get maximum values of lengths:

res = wdata['sentences'].str.split().str.len().max()

print("The maximum length in words are : " +  str(res)) 

#solution with f-strings
print(f"The maximum length in words are : {res}") 

length = wdata['sentences'].str.len().max()

print("The maximum length in chars are : " +  str(length)) 

#solution with f-strings
print("fThe maximum length in chars are : {length}") 
Related