繁体   English   中英

如何在pandas中获取lambda函数内元素的索引

[英]How to get the index of elements inside lambda function in pandas

我有一个 Pandas 数据框(称为 df),因此其中一列称为收入。 我只是想使用 lambda 函数更改此列的元素,如下所示,但我在访问元素的索引时遇到问题:

df['Revenue']=df['Revenue'].apply(lambda d: Output:(a function of index of d in Revenue) Conditional statement)

我需要将d的索引放在 lambda 函数中,但不知道该怎么做。

你可以分析这个例子。 资源

# importing pandas and numpylibraries
import pandas as pd
import numpy as np
 
# creating and initializing a nested list
values_list = [[15, 2.5, 100], [20, 4.5, 50], [25, 5.2, 80],
               [45, 5.8, 48], [40, 6.3, 70], [41, 6.4, 90],
               [51, 2.3, 111]]
 
# creating a pandas dataframe
df = pd.DataFrame(values_list, columns=['Field_1', 'Field_2', 'Field_3'],
                  index=['a', 'b', 'c', 'd', 'e', 'f', 'g'])
 
 
# Apply function numpy.square() to square
# the values of 3 rows only i.e. with row
# index name 'a', 'e' and 'g' only
df = df.apply(lambda x: np.square(x) if x.name in [
              'a', 'e', 'g'] else x, axis=1)
df

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM