I'm trying to use either locals() or *args to iterate over multiple function arguments. However, I am defining the function arguments as columns from a dataframe. How do I edit the following to have the float_format function iterate over a variable number of arguments?
#! /usr/bin/env python3
import pandas as pd
def float_format(a, b, c, d, e, f): #Change to single *args function argument?
for x in range(len(data[a])):
data[a][x] = data[a][x].replace(' Mbps', '')
for x in range(len(data[b])):
data[b][x] = data[b][x].replace(' Mbps', '')
for x in range(len(data[c])):
data[c][x] = data[c][x].replace(' Mbps', '')
for x in range(len(data[d])):
data[d][x] = data[d][x].replace(' Mbps', '')
for x in range(len(data[e])):
data[e][x] = data[e][x].replace(' Mbps', '')
for x in range(len(data[f])):
data[f][x] = data[f][x].replace(' Mbps', '')
file = r'Original_File.xls'
data = pd.read_excel(file, header=[2])
float_format('Average Receive bps',
'Peak Receive bps',
'Received Bandwidth',
'Average Transmit bps',
'Peak Transmit bps',
'Transmit Bandwidth')
data.to_excel('results.xlsx', 'w+')
So if I try
def float_format(*iterate):
for arg in iterate:
for x in range(len(data[iterate])):
data[iterate][x] = data[iterate][x].replace(' Mbps', '')
I'm getting traceback errors over the way the function runs.
example df
>>> data
Display Name Interface Name ... Peak Transmit bps Transmit Bandwidth
0 1951 - LAB - FW1 port1 ... 0.56 Mbps 10.00 Mbps
1 1951 - LAB - FW1 port1 ... 0.37 Mbps 10.00 Mbps
2 1951 - LAB - FW1 port1 ... 0.34 Mbps 10.00 Mbps
3 1951 - LAB - FW1 port1 ... 0.36 Mbps 10.00 Mbps
4 1951 - LAB - FW1 port1 ... 0.83 Mbps 10.00 Mbps
5 1951 - LAB - FW1 port1 ... 0.55 Mbps 10.00 Mbps
6 1951 - LAB - FW1 port1 ... 0.27 Mbps 10.00 Mbps
7 1951 - LAB - FW1 port1 ... 0.41 Mbps 10.00 Mbps
8 1951 - LAB - FW1 port2 ... 0.00 Mbps 1000.00 Mbps
9 1951 - LAB - FW1 port2 ... 0.00 Mbps 1000.00 Mbps
10 1951 - LAB - FW1 port2 ... 0.00 Mbps 1000.00 Mbps
11 1951 - LAB - FW1 port2 ... 0.00 Mbps 1000.00 Mbps
12 1951 - LAB - FW1 port2 ... 0.00 Mbps 1000.00 Mbps
13 1951 - LAB - FW1 port2 ... 0.00 Mbps 1000.00 Mbps
14 1951 - LAB - FW1 port2 ... 0.19 Mbps 1000.00 Mbps
15 1951 - LAB - FW1 port2 ... 0.31 Mbps 1000.00 Mbps
for i in [a,b,c,d,e,f]. Or if you want a variadic function, indeed use*args.*args?