teller.py3
teller.py3

Reputation: 844

Find length of the longest column in pandas

Currently I have the following dataframe:

data = {'shoe': ['a', 'b'], 'fury': ['c','d','e','f'], 'chaos': ['g','h', 'i']}
dataFrame = pandas.DataFrame({k:pandas.Series(v) for k, v in data.items()})

Output:

  shoe fury chaos
0    a    c     g
1    b    d     h
2  NaN    e     i
3  NaN    f   NaN

Is there a way to find the length of the longest column in a dataframe? In this case this should be 4. Does pandas have a method available for similar purposes?

Thanks for reading

Upvotes: 3

Views: 1800

Answers (6)

jpp
jpp

Reputation: 164773

You can calculate the last valid index:

LVI = df.last_valid_index()         # 3

To get the length of the longest column, you can use pd.Index.get_loc:

length = df.index.get_loc(LVI) + 1  # 4

If your index is the default pd.IndexRange, then you can simply use LVI + 1.

Upvotes: 0

U13-Forward
U13-Forward

Reputation: 71610

@student's and @Wen's answers are the best but a python solution would be:

print(len(max(dataFrame,key=lambda x: dataFrame[x].count())))

I definitely wouldn't recommend this, because it's inefficient, this is the last option i will choose :-), at least it works :-)

Upvotes: 2

Dodge
Dodge

Reputation: 3309

You can apply a lambda to your vectors:

df.apply(lambda x: len(x.dropna()))

chaos    3
fury     4
shoe     2
dtype: int64

df.apply(lambda x: len(x.dropna())).max()
4

Upvotes: 1

sacuL
sacuL

Reputation: 51395

@student's answer is better, but as an alternative:

>>> dataFrame.notnull().sum(0).max()
4

Upvotes: 0

BENY
BENY

Reputation: 323326

Since you create the dataframe via dict , which means the longest columns is equal to the length of df,so

len(df)
Out[368]: 4

Upvotes: 3

niraj
niraj

Reputation: 18218

You can try using count followed by max. According to pandas documentation for the count:

Count non-NA cells for each column or row.

print(dataFrame.count().max())

Upvotes: 5

Related Questions