LoginSignup
2
4

More than 1 year has passed since last update.

[Python] [EXCEL] 結合セルをdataframeに読み込んで、欠損値の各行に値を入れる ☆便利☆

Last updated at Posted at 2022-06-20

エクセルで複数のセルが結合されているものを読み込む時と、先頭の行のみに
値が入り、他はNaTになってしまいます。
(このような結合されたエクセル)
スクリーンショット 2022-06-20 174854.png

Pandasで読み込むと・・

import pandas as pd
df=pd.read_excel('MergedCellTable.xlsx') # 上のファイル名です

スクリーンショット 2022-06-20 181608.png
そこで、結合されていた列に対してmethod='ffill'でfillnaします。

df['Date']=df['Date'].fillna(method='ffill')
df['City']=df['City'].fillna(method='ffill')

スクリーンショット 2022-06-20 181837.png
エクセルデータ取り込み時のデータの前処理に便利です!

2
4
1

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
2
4