2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

メーカー営業xPython(売上前年同月比表)

Posted at

はじめに:

本投稿はメーカー営業xPythonがテーマになります。
メーカー営業マンが常日頃エクセルで行う?作業をPythonにて置き換え挑戦備忘録です。

内容

担当製品の受注動向分析を行う際、前年同月値を並列させ動向把握を行う。
しかし社内インフラでは受注データを落とす際、下記のようなフォーマットで出力される。
この「不便なデータ」をVBAで「完成形」へとデータ整形を行っていた。
*例では3製品ですが、実務では数千製品に及びます。
⇨上記をPythonでの置き換えに挑戦。
【例:2020年データ】
image.png
【例:2019年データ】
image.png
【完成形】
image.png

# Pandasのインポート
import pandas as pd
# エクセルの読み込み
df1=pd.read_excel('/Users/2019V2.xlsx')
df2=pd.read_excel('/Users/2020V2.xlsx')
df1.set_index(['製品'],inplace=True)
df2.set_index(['製品'],inplace=True)
	2019-01-01 00:00:00	2019-02-01 00:00:00	2019-03-01 00:00:00	2019-04-01 00:00:00	2019-05-01 00:00:00	2019-06-01 00:00:00
製品						
A	200	54	345	665	78	676
B	456	675	448	453	676	67
C	234	675	5757	578	789	141

2020-01-01 00:00:00	2020-02-01 00:00:00	2020-03-01 00:00:00	2020-04-01 00:00:00	2020-05-01 00:00:00	2020-06-01 00:00:00
製品						
C	57	177	788	665	78	175
B	678	78	967	787	471	263
A	678	353	46	479	742	787
# 行列を入れ替えた後、月を抜き出し、インデックスへ設定
df3=df1.T
df4=df2.T
df3['month'] = list(pd.Series(df3.index).apply(lambda x: x.month))
df4['month'] = list(pd.Series(df4.index).apply(lambda x: x.month))
df5=df3.set_index(['month'])
df6=df4.set_index(['month'])
製品	A	B	C
month			
1	200	456	234
2	54	675	675
3	345	448	5757
4	665	453	578
5	78	676	789
6	676	67	141

製品	C	B	A
month			
1	57	678	678
2	177	78	353
3	788	967	46
4	665	787	479
5	78	471	742
6	175	263	
# DataFrame同士の割り算を行う。
# コラムとインデックスが共通のするもの同士で四則演算される。(その為上の処理で月を抜き出した。)
df7=df6/df5
製品	A	B	C
month			
1	3.390000	1.486842	0.243590
2	6.537037	0.115556	0.262222
3	0.133333	2.158482	0.136877
4	0.720301	1.737307	1.150519
5	9.512821	0.696746	0.098859
6	1.164201	3.925373	1.241135
# 列行の入れ替え
df8=df5.T
df9=df6.T
df10=df7.T
# concatで結合
df11=pd.concat([df8,df9,df10])
month	1	2	3	4	5	6
製品						
A	200.000000	54.000000	345.000000	665.000000	78.000000	676.000000
B	456.000000	675.000000	448.000000	453.000000	676.000000	67.000000
C	234.000000	675.000000	5757.000000	578.000000	789.000000	141.000000
C	57.000000	177.000000	788.000000	665.000000	78.000000	175.000000
B	678.000000	78.000000	967.000000	787.000000	471.000000	263.000000
A	678.000000	353.000000	46.000000	479.000000	742.000000	787.000000
A	3.390000	6.537037	0.133333	0.720301	9.512821	1.164201
B	1.486842	0.115556	2.158482	1.737307	0.696746	3.925373
C	0.243590	0.262222	0.136877	1.150519	0.098859	1.241135
# 製品名毎に並び替えを行う。(同一品名はconcat順で整列される)
df12=df11.sort_index()
month	1	2	3	4	5	6
製品						
A	200.000000	54.000000	345.000000	665.000000	78.000000	676.000000
A	678.000000	353.000000	46.000000	479.000000	742.000000	787.000000
A	3.390000	6.537037	0.133333	0.720301	9.512821	1.164201
B	456.000000	675.000000	448.000000	453.000000	676.000000	67.000000
B	678.000000	78.000000	967.000000	787.000000	471.000000	263.000000
B	1.486842	0.115556	2.158482	1.737307	0.696746	3.925373
C	234.000000	675.000000	5757.000000	578.000000	789.000000	141.000000
C	57.000000	177.000000	788.000000	665.000000	78.000000	175.000000
C	0.243590	0.262222	0.136877	1.150519	0.098859	1.241135
# ヒートマップを作成する為にインデックスのリセット。(インデックスの製品名が重複しているとstyleを使用できない為)
df13=df12.reset_index()
display(df13.style.background_gradient(axis=1))

image.png

結論:

体裁は微妙に異なるが、手間隙を考えるとPythonに軍配が上がりそう。
特にデーターフレーム同士で四則演算が行えるのは非常に便利👏
ただマクロを一度作ってしまえばマクロの方が便利かも??

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?