I applied normalization on multiple columns in Pandas dataframe by using for-loop under the condition of below:
Normalization for A , B columns between : [-1 , +1]
Normalization for C column between : [-40 , +150]
and replace results in alternative dataframe let's call norm_data
and store it as a csv file.
my data is txt file dataset
# Import and call the needed libraries
import numpy as np
import pandas as pd
#Normalizing Formula
def normalize(value, min_value, max_value, min_norm, max_norm):
new_value = ((max_norm - min_norm)*((value - min_value)/(max_value - min_value))) + min_norm
return new_value
#Split data in three different lists A, B and C
df1 = pd.read_csv('D:\me4.TXT', header=None)
id_set = df1[df1.index % 4 == 0].astype('int').values
A = df1[df1.index % 4 == 1].values
B = df1[df1.index % 4 == 2].values
C = df1[df1.index % 4 == 3].values
data = {'A': A[:,0], 'B': B[:,0], 'C': C[:,0]} # arrays
#df contains all the data
df = pd.DataFrame(data, columns=['A','B','C'], index = id_set[:,0])
df2 = pd.DataFrame(data, index= id_set[0:])
print(df)
#--------------------------------
cycles = int(len(df)/480)
print(cycles)
#next iteration create all plots, change the numer of cycles
for i in df:
min_val = df[i].min()
max_val = df[i].max()
if i=='C':
#Applying normalization for C between [-40,+150]
data['C'] = normalize(df[i].values, min_val, max_val, -40, 150)
elif i=='A':
#Applying normalization for A , B between [-1,+1]
data['A'] = normalize(df[i].values, min_val, max_val, -1, 1)
else:
data['B'] = normalize(df[i].values, min_val, max_val, -1, 1)
norm_data = pd.DataFrame(data)
print(norm_data)
norm_data.to_csv('norm.csv')
df2.to_csv('my_file.csv')
print(df2)
Problem is after normalization by help of @Lucas I've missed my index was labeled id_set
.
So far I got below output in my_file.csv including this error TypeError
unsupported format string passed to numpy.ndarray.__format__:
id_set A B C
['0'] 2.291171 -2.689658 -344.047912
['10'] 2.176816 -4.381186 -335.936524
['20'] 2.291171 -2.589725 -342.544885
['30'] 2.176597 -6.360999 0.000000
['40'] 2.577268 -1.993412 -344.326376
['50'] 9.844076 -2.690917 -346.125859
['60'] 2.061782 -2.889378 -346.378859
['70'] 2.348300 -2.789547 -347.980986
['80'] 6.973350 -1.893454 -337.884738
['90'] 2.520040 -3.087004 -349.209006
which those ['']
are unwanted!
my desired output should be like below after normalization :
id_set A B C
000 -0.716746 0.158663 112.403310
010 -0.726023 0.037448 113.289702
020 -0.716746 0.165824 112.567557
030 -0.726040 -0.104426 150.000000
040 -0.693538 0.208556 112.372881
050 -0.104061 0.158573 112.176238
060 -0.735354 0.144351 112.148590
070 -0.712112 0.151505 111.973514
080 -0.336932 0.215719 113.076807
090 -0.698181 0.130189 111.839319
010 0.068357 -0.019388 114.346421
011 0.022007 0.165824 112.381444
Any ideas would be welcome since it's important data for me.