Bijan
Bijan

Reputation: 8594

Python one-liner for loop with if statement

I have a function used for my Pandas dataframe to return the length of each column:

def get_col_widths(dataframe)
    return [max([len(str(s)) for s in dataframe[col].values] + [len(col)]) for col in dataframe.columns]

However I have a column of dates (of type numpy.datetime64) so when you do len(str(s)) it returns 29 instead of the expected 10 (MM/DD/YYYY).

How can I incorporate an if to test if isinstance(s, np.datetime64), return 10 instead of len(str(s))?

Upvotes: 1

Views: 127

Answers (3)

Guimoute
Guimoute

Reputation: 4629

You just slip the condition before for s like so:

def get_col_widths(dataframe):
     return [max([(len(str(s)) if not isinstance(s, np.datetime64) else 10) for s in dataframe[col].values] + [len(col)]) for col in dataframe.columns]

But I would advise against using a one-liner. There is no need to compact everything. Readable code is better.

Upvotes: 1

BENY
BENY

Reputation: 323226

We do not need for loop here

c=col

df[c].astype(str).str.len().max()

Upvotes: 1

Red
Red

Reputation: 27547

Like this:

def get_col_widths(dataframe):
    return [max([len(str(s)) if not isinstance(s, np.datetime64) else 10 for s in dataframe[col].values] + [len(col)]) for col in dataframe.columns]

Upvotes: 1

Related Questions