LoginSignup
0
0

More than 1 year has passed since last update.

Pythonで「Excelの実質行数」を知りたい件

Last updated at Posted at 2023-03-27

やりたいこと

Excelで

No  Data1 Data2  
 1  AAA   aaa
 2  BBB   bbb
 3  CCC   ccc
 4  DDD   ddd
 5  EEE   eee
 6  FFF   fff
 7  GGG   ggg
 8  HHH   hhh
 9
10
11
12
...
98
99

みたいに"No"だけがずっと先のNoで定義されているが実質はもっと少ないデータを、実質の部分だけ抜き出したい。
今回だと、行数が99行と言われるのではなく、行数は8行と言ってほしい。
※元Excelは、別の作成者が作ったもののため 編集不可 である点に注意。

参考文献

今回はChatGPTに書き方を聞いた。ChatGPTはいいぞ。

ソリューション

#-------------------------------------------------
# 「列名」の中にデータが入っている行番号の最大
# ※Excelでindexだけが多数定義されているが実際のデータが無い場合の対応
# df  : DataFrame
# col_str: 列名
#-------------------------------------------------
def get_max_row_num_with_contain_data(df, col_str):
    print(" -> get_max_row_num_with_contain_data: " + str(col_str))
    row_num = df.index[df[col_str].notnull()].max() +1
    return row_num
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0