在 Pandas 的列上应用 lambda

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41848480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:51:48  来源:igfitidea点击:

Apply lambda on a column in pandas

pythonpandaslambdapandas-apply

提问by Shiva Krishna Bavandla

I have the below data frame

我有以下数据框

ipdb> csv_data
  country_edited  sale_edited  date_edited  transformation_edited
0          India       403171     21091956                      1
1         Bhutan       394096     21091956                      2
2          Nepal       361372     21091956                      3
3         madhya       355883     21091956                      4
4          sudan       262949     21091956                      5

and below is my code

下面是我的代码

transfactor_count = 5.6
csv_data["transformation_edited"] = csv_data["transformation_edited"].apply(lambda x: x * transfactor_count)

But the above code was giving me an error

但是上面的代码给了我一个错误

*** NameError: global name 'transfactor_count' is not defined

How to solve this ?

如何解决这个问题?

Actual code

实际代码

for foreign_key in data_mapping_record.csvdatabasecolumnmapping_set.all():
    data_type = foreign_key.data_type
    trans_factor = foreign_key.tranformation_factor
    if data_type == "Decimal":
        import ipdb; ipdb.set_trace()
        csv_data[foreign_key.table_column_name] = csv_data[foreign_key.table_column_name].apply(lambda x: x * trans_factor )
    elif data_type in ["Date", "Datetime"]:
        csv_data[foreign_key.table_column_name] = csv_data[foreign_key.table_column_name].apply( lambda d: datetime.strptime(d, dates[date]) )

回答by Jan Zeiseweis

As the error already says, the lambda function can't find the globalvariable. You can try make it global by:

正如错误所说,lambda 函数找不到全局变量。您可以尝试通过以下方式使其成为全球性的:

global transfactor_count
transfactor_count = 5.6
csv_data["transformation_edited"] = csv_data["transformation_edited"].apply(lambda x: x * transfactor_count)

But as jezrael pointed out:

但正如 jezrael 指出的那样:

csv_data["transformation_edited"] = csv_data["transformation_edited"] * transfactor_count 

is much more elegant.

更优雅。

you might have to change the datatype to float before:

您可能必须在之前将数据类型更改为浮动:

csv_data["transformation_edited"] = csv_data["transformation_edited"].astype(float) * transfactor_count

回答by sandeep

You can use lambda on dataframe as below:

您可以在数据帧上使用 lambda,如下所示:

transfactor_count = 5.6;
csv_data['transformation_edited']=map(lambda x: x * transfactor_count, csv_data['transformation_edited'])