Python 如何使用pandas计算某列的excel行号

Python 如何使用pandas计算某列的excel行号,python,excel,pandas,dataframe,Python,Excel,Pandas,Dataframe,我想用熊猫来计算特定列的行号。 我有这样的数据帧: A B C 0 1 a a 1 2 b b 2 3 c c 3 4 NaN NaN 4 5 NaN NaN 5 6 NaN NaN 6 7 NaN NaN 7 8 NaN NaN 8 9 NaN NaN df = pd.read_excel(excel_file_path, sheetname="test", index_col="1") prin

我想用熊猫来计算特定列的行号。 我有这样的数据帧:

   A    B    C
0  1    a    a
1  2    b    b
2  3    c    c
3  4  NaN  NaN
4  5  NaN  NaN
5  6  NaN  NaN
6  7  NaN  NaN
7  8  NaN  NaN
8  9  NaN  NaN
df = pd.read_excel(excel_file_path, sheetname="test", index_col="1")
print(len(df))
print(df)
我想计算B列中的行数,我的代码如下所示:

   A    B    C
0  1    a    a
1  2    b    b
2  3    c    c
3  4  NaN  NaN
4  5  NaN  NaN
5  6  NaN  NaN
6  7  NaN  NaN
7  8  NaN  NaN
8  9  NaN  NaN
df = pd.read_excel(excel_file_path, sheetname="test", index_col="1")
print(len(df))
print(df)
但是不管什么
索引列
我都会把它放在最长列的行号上


我应该在代码中做什么更改才能获得B列的o行数,但没有空行?

使用
last\u valid\u index

df.apply(lambda x : x.last_valid_index()).add(1)
Out[440]: 
A    9
B    3
C    3
dtype: int64

通过使用
last\u valid\u索引

df.apply(lambda x : x.last_valid_index()).add(1)
Out[440]: 
A    9
B    3
C    3
dtype: int64

df.B.count()@耶斯雷尔:是的。你说得对~:@Bharathshetty哪个答案?Sry…@Bharathshetty不,我已经用过一口井了…但是
df.count()
是最好的;)@耶斯雷尔:是的。你说得对~:@Bharathshetty哪个答案?Sry…@Bharathshetty不,我已经用了一口井了。。。