Member-only story

Pythonで神エクセルをなんとかするシリーズ

複数のカラムのデータを条件にしてフラグを立てる方法

Naoki Satoh
Apr 8, 2023

こういうデータがあって。目的が二つのサーバーに入っているアプリケーションのソフトウェアの差異を検出することだとします(実際に類似の依頼があった)

要件としては同じアプリケーション同士でTigerサーバーに入っているソフトウェア構成がLionサーバと同じかどうかを調べたい。Tigerサーバーを中心にアプリケーションをキーにマージするとこうなる’

import pandas as pd
from tkinter import filedialog
fle = filedialog.askopenfilename()
df = pd.read_excel(fle)
df_tig = df.loc[df['Servers'] == 'Tiger']
df_lio = df.loc[df['Servers']== 'Lion']
df01 = pd.merge(df_tig, df_lio, on=['Softwa','Application'], how = 'left')
print(df01)

この方法でTigerサーバに入っているがLionサーバに入っていないものがServers_y列を見るとわかる。のだけどこれだとLionサーバーにはソフトウェアが入っていないのか、それともバージョンが違うのかがわからない。

そこでMergeのメソッドを左結合から外部結合に変えてみると

df = pd.read_excel(fle)
df_tig = df.loc[df['Servers'] == 'Tiger']
df_lio = df.loc[df['Servers']== 'Lion']
df01 = pd.merge(df_tig, df_lio, on=['Software','Application'], how = 'outer')
print(df01)

出力はこう。こうするとServers_xとServers_yに名前が入っていれば両方同一のソフトウェアが入っていて、Tigerだけの場合はTigerだけ、Lionだけの場合はLionだけというのが…

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Naoki Satoh
Naoki Satoh

Written by Naoki Satoh

オランダ在住 セキュリティエンジニア。専門はIT監査、IT統制、リスク。よわよわPythonista。コンサドーレ札幌サポ。タッチラグビー。語学マニア。マストドン mstdn.jp/@naokyneko Pixel7 Instagram www.instagram.com/naokisatoh_nl

No responses yet

Write a response