[英]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.