I have a CSV file that looks like this:
Build,Avg,Min,Max
BuildA,56.190,39.123,60.1039
BuildX,57.11,40.102,60.200
BuildZER,55.1134,35.129404123,60.20121
I want to get the average, min, max of each column and have each of these stats be as a new row. I exclude the non numeric column (the build column) and then run the statistics. I accomplish this by doing:
df = pd.read_csv('fakedata.csv')
columns = []
builds = []
for column in df.columns:
if(df[column].dtype == 'float64'):
columns.append(column)
else:
builds.append(column)
save = df[builds]
df = df[columns]
print(df)
df.loc['Min']= df.min()
df.loc['Average']= df.mean()
df.loc['Max']= df.max()
If I were then to write this data to a CSV it would look like:
,Avg,Min,Max
0,56.19,39.123,60.1039
1,57.11,40.102,60.2
2,55.1134,35.129404123,60.20121
Min,55.1134,35.129404123,60.1039
Average,55.8817,37.3709520615,60.1522525
Max,57.11,40.102,60.20121
Which is close to what I want but I want the Build column to be column one again and have the build names exist on top of the Min, Average, Max. Basically this:
Builds,Avg,Min,Max
BuildA,56.19,39.123,60.1039
BuildX,57.11,40.102,60.2
BuildZER,55.1134,35.129404123,60.20121
Min,55.1134,35.129404123,60.1039
Average,55.8817,37.3709520615,60.1522525
Max,57.11,40.102,60.20121
I have attempted to accomplish this by doing:
df.insert(0,'builds', save)
with open('fakedata.csv', 'w') as f:
df.to_csv(f)
But this gives me this CSV:
,builds,Avg,Min,Max
0,Build1,56.19,39.123,60.1039
1,Build2,57.11,40.102,60.2
2,Build3,55.1134,35.129404123,60.20121
Min,,55.1134,35.129404123,60.1039
Average,,55.8817,37.3709520615,60.1522525
Max,,57.11,40.102,60.20121
How can I fix this?