MM55
MM55

Reputation: 65

How can I solve this in pandas?

I have the data flowing into a csv file daily which shows the no. of pieces being manufactured. I want to clearly show the daily % increase in pieces being produced

I have tried transpose(), unstack() but have not been able to solve this.

Here is what the data looks like:

enter image description here

I want to clearly show the daily % increase in pieces being produced. The output should be something like this:

enter image description here

How should I get this done?

Upvotes: 0

Views: 91

Answers (3)

anky
anky

Reputation: 75100

You would need s.pct_change() and series.shift():

df.insert(1,'Day2',df.Day.shift(-1))
df['Percent_change']=(df.Peice_Produced.pct_change()*100).shift(-1).fillna(0).round(2)
print(df)

        Day     Day2  Peice_Produced  Percent_change
0    1/1/17   1/2/17              10          -50.00
1    1/2/17   1/3/17               5          200.00
2    1/3/17   1/4/17              15          -60.00
3    1/4/17   1/5/17               6          250.00
4    1/5/17   1/6/17              21          -66.67
5    1/6/17   1/7/17               7          300.00
6    1/7/17   1/8/17              28          -71.43
7    1/8/17   1/9/17               8          350.00
8    1/9/17  1/10/17              36          -75.00
9   1/10/17  1/11/17               9          400.00
10  1/11/17      NaN              45            0.00

Upvotes: 2

Karn Kumar
Karn Kumar

Reputation: 8826

I admit I do not fully understand what your intent is. Nevertheless, I may have a solution as i understand it ..

Use diff() function to find the discrete difference

Your Simulated DataFarme:

>>> df
        Day  Peice_Produced
0    1/1/17              10
1    1/2/17               5
2    1/3/17              15
3    1/4/17               6
4    1/5/17              21
5    1/6/17               7
6    1/7/17              28
7    1/8/17               8
8    1/9/17              36
9   1/10/17               9
10  1/11/17              45

Solution: One way around of doing..

>>> df['Day_over_day%'] = df.Peice_Produced.diff(periods=1).fillna(0).astype(str) + '%'
>>> df
        Day  Peice_Produced Day_over_day%
0    1/1/17              10          0.0%
1    1/2/17               5         -5.0%
2    1/3/17              15         10.0%
3    1/4/17               6         -9.0%
4    1/5/17              21         15.0%
5    1/6/17               7        -14.0%
6    1/7/17              28         21.0%
7    1/8/17               8        -20.0%
8    1/9/17              36         28.0%
9   1/10/17               9        -27.0%
10  1/11/17              45         36.0%

Upvotes: 1

Polkaguy6000
Polkaguy6000

Reputation: 1208

You can just add a calculated column. I'm assuming you are storing this data in a pandas DataFrame called df. You can do this simply with:

 df['change'] = (df['Pieces Produced'] / df['Pieces Produced'].shift(1))-1

Upvotes: 0

Related Questions