gpt4 book ai didi

python - 如何迭代多个函数参数?

转载 作者:行者123 更新时间:2023-12-02 00:29:57 30 4
gpt4 key购买 nike

我尝试使用 locals() 或 *args 来迭代多个函数参数。但是,我将函数参数定义为数据框中的列。如何编辑以下内容以使 float_format 函数迭代可变数量的参数?

#! /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+')

所以如果我尝试

def float_format(*iterate):
for arg in iterate:
for x in range(len(data[iterate])):
data[iterate][x] = data[iterate][x].replace(' Mbps', '')

我在函数运行的过程中遇到了回溯错误。

示例 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

最佳答案

下面这个怎么样?

def test(*argv):
for arg in argv:
print(arg)

test('hello', 'world')
# hello
# world

关于python - 如何迭代多个函数参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60063867/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com