Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find maximum string length by column in data frame

Tags:

string

r

max

My question is similar to this. But for strings.

So I have a dataframe, each column contains strings of different length. So, how I can find the maximum string length per column?

Then, how to select the columns, where length is > 1, by sapply or similar.

A typical column of the dataframe looks like this:

clmn=c("XDX", "GUV", "FQ", "ACUE", "HIT", "AYX", "NFD", "AHBW", "GKQ", "PYF")

Thanks

like image 502
Kalin Stoyanov Avatar asked Mar 05 '16 19:03

Kalin Stoyanov


People also ask

How do I find the length of a string in a DataFrame column?

To find the length of strings in a data frame you have the len method on the dataframes str property. But to do this you need to call this method on the column that contains the string data.

How do you find the maximum of a column in a data frame?

The max() method returns a Series with the maximum value of each column. By specifying the column axis ( axis='columns' ), the max() method searches column-wise and returns the maximum value for each row.

How do you find the longest string in a column in Python?

To find the length of the longest string in a DataFrame column, use the expression df. COL. str. len().

Why the length () function is used in data frame?

len() function is used to compute the length of each element in the Series/Index. Compute the length of each element in the Series/Index. The element may be a sequence (such as a string, tuple or list) or a collection (such as a dictionary).


1 Answers

We can use nchar

max(nchar(clmn))

For finding the maximum character length for each column

lapply(df1, function(x) max(nchar(x)))

If we need to filter the columns that have maximum string length greater than 1

df1[sapply(df1, function(x) max(nchar(x)))>1]

Or

Filter(function(x) max(nchar(x)) >1, df1)
like image 159
akrun Avatar answered Nov 07 '22 12:11

akrun