見出し画像

【Python】read_pickleからデータフレームをマージして縦に結合し、重複を削除するサンプル【Colaboratory】

Google Drive

from google.colab import drive 
drive.mount('/content/drive/')
%cd "/content/drive/My Drive/googleseo/data"

現在のフォルダ内のファイル一覧を取得確認(.pkl全て)

import glob
import pandas as pd
files = glob.glob("*.pkl")
for file in files:
  print(file)

空のデータフレーム作って、そこに読み込んだピッケルファイルを結合していく

df = pd.DataFrame()
files = glob.glob("*.pkl")
for file in files:
  df_pkl = pd.read_pickle(file)
  df = pd.concat([df, df_pkl],
                 axis=0,
                 ignore_index=True
                 )

dfから重複行を削除する

df = df[~df.duplicated()]

テキストファイルとして出力

df.to_csv("marge.txt", sep=",", index=False, header=False)


いつもお読みいただき、ありがとうございます。 書くだけでなく読みたいので、コメント欄で記事名入れてもらうと見に行きます。