【问题标题】:How to count excel row number for certain column with pandas [duplicate]如何用熊猫计算某些列的excel行号[重复]
【发布时间】:2018-03-13 17:12:45
【问题描述】:

我想用 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

我想计算 B 列的行数,我的代码如下所示:

df = pd.read_excel(excel_file_path, sheetname="test", index_col="1")
print(len(df))
print(df)

但不管index_col是什么我都会把它总是返回最长列的行号。

我应该在代码中更改什么以获得 B 列的 o 行但没有空行?

【问题讨论】:

标签: python excel pandas dataframe


【解决方案1】:

通过使用last_valid_index

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

【讨论】:

  • 但是df.count() 是最好的 ;)
  • @jezrael 是的。你是对的~:)
  • @Bharathshetty 哪个答案?对不起...
  • @Bharathshetty nope,我一直在用这个...
猜你喜欢
  • 1970-01-01
  • 2021-11-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-10
  • 1970-01-01
  • 2019-10-14
  • 2016-06-05
  • 2020-06-12
相关资源
最近更新 更多