我有包含浮动月份数据的csv,它是随机生成的。
我想比较 2 个月并将 % 值放在中间。如果我进行比较,则放在最后。
import pandas as pd
import numpy as np
df = pd.read_csv('Services.csv').fillna(0)
df.drop(df.columns[:1],axis=1,inplace=True)
a = len(df.columns)
for col in range(a-1):
current = (df.iloc[:, col])
col += 1
per = ("per" , col)
previous = (df.iloc[:, col])
per = []
for a, b in zip(current, previous):
try:
x = repr(round((a - b) / a * 100.0))
per.append(x + "%")
except ZeroDivisionError:
per.append(0)
df['Variation %'] = per
print (df) ```
---
What I am getting is :
June July September August Variation %
0 0.000000e+00 0.000000 0.000000 0.000000 0
1 -8.840000e-04 0.137259 1.215444 1.378786 15627%
2 -2.500000e-02 0.697000 0.438000 0.834000 2888%
3 0.000000e+00 0.162507 0.000000 0.000000 0
4 0.000000e+00 0.000000 0.000000 0.000000 0
5 2.000000e-04 0.000855 0.000000 0.000000 -328%
6 -6.661338e-16 0.000000 8.878000 6.405564 100%
7 1.216297e+01 0.029005 0.000000 0.025500 100%
what I am looking for in format like, it should compare between 2 month and place it to next column.
ex (As an image attached):
``` July Variation% August Variation% September Variation% October
0.32 98% 17.58 98% 17.58 98% 17.58
