ignore_index=Trueとは

2020年12月21日

ignore_index=Trueとは

ignore_indexとは何でしょうか?indexを無視するという意味になりますね。concatでデータフレームを結合した際にindexに付与された番号を無視すると言う事になります。データで見てみましょう。

参考:Pandas のデータフレームに行や列 (カラム) を追加する


fruits = [
    {'item': 'apple', 'price': 200},
    {'item': 'orange', 'price': 300},
    {'item': 'banana', 'price': 150}
]
vagetables = [
    {'item': 'tomato', 'price': 200},
    {'item': 'carrot', 'price': 300},
    {'item': 'cabbage', 'price': 400}
]
df_fruits = pd.DataFrame(fruits)
df_vagetables = pd.DataFrame(vagetables)

df = pd.concat([df_fruits, df_vagetables], ignore_index=False)
#結果
      item  price
0    apple    200
1   orange    300
2   banana    150
0   tomato    200
1   carrot    300
2  cabbage    400

インデックスが「012012」となっていますね。
それでは「ignore_index=True」も見てみましょう。


...
df = pd.concat([df_fruits, df_vagetables], ignore_index=True)
#結果
      item  price
0    apple    200
1   orange    300
2   banana    150
3   tomato    200
4   carrot    300
5  cabbage    400

インデックスが連番で「012345」になりましたね。

YouTube

2020年12月21日