Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (50.2k points)

I have a dataframe like this:

image

I am calculation length of lists in the CreationDate column and making a new Length column like this:

df['Length'] = df.CreationDate.apply(lambda x: len(x))

Which gives me this:

image

Is there a more pythonic way to do this?

1 Answer

0 votes
by (107k points)

You can utilize the str accessor for some list operations as well. For instance:

df['CreationDate'].str.len()

returns the length of each list. Refer the following link for str.len:

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.str.len.html

If you wish to Learn more about Pandas visit this Pandas Tutorial.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...