在for循环中使用value_counts创建函数

时间:2020-01-16 03:19:55

标签: function loops

我正在尝试创建一个函数,该函数为我通过它的列表中的每个变量调用value_counts函数。诚然,我不知道自己在做什么,并且以简化的“ hello world”教程为例。我的示例代码已随附。谢谢您的帮助。

data1.columns = ['Year', 'Rank', 'Company', 'Revenue', 'Profit']

def vc(*args):
    for x in args:
        value_counts()
    return

vc(['Year', 'Rank', 'Company', 'Revenue', 'Profit'])

我也尝试过

def vc(args):
    for x in args:
        value_counts(x)
    return

vc(['Year', 'Rank', 'Company', 'Revenue', 'Profit'])

我收到一条错误消息,指出未定义value_counts。

1 个答案:

答案 0 :(得分:0)

您没有将import "package:flutter/material.dart"; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { Widget build(BuildContext context) { return MaterialApp( home: HomePage(), ); } } class HomePage extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( body: Center( child: Padding( padding: const EdgeInsets.all(30.0), child: ClipRRect( borderRadius: BorderRadius.circular(5.0), child: Container( height: 100.0, margin: const EdgeInsets.only(bottom: 6.0), //Same as `blurRadius` i guess decoration: BoxDecoration( borderRadius: BorderRadius.circular(5.0), color: Colors.white, boxShadow: [ BoxShadow( color: Colors.grey, offset: Offset(0.0, 1.0), //(x,y) blurRadius: 6.0, ), ], ), ), ), ), ), ); } } 传递给x

value_counts

应该是

for x in args:
        value_counts()

此外,for x in args: value_counts(x) 具有vc参数,这意味着您不需要为其提供列表。您可以使用*args进行呼叫,当您在vc('Year', 'Rank', 'Company', 'Revenue', 'Profit')中引用args时,将得到一个列表。您可以像现在一样使用列表来调用vc,但是您可以将vc的定义更改为def vc(*args),以便所有内容匹配。

有帮助吗?