简体   繁体   English

我应该如何构造一个采用 pandas dataframe 并写入其列的 function 并写入其列?

[英]How should i structure a function that takes a pandas dataframe and writes to its columns?

I want to write a function that iterates through a dataframe, and takes each row's value as an argument.我想编写一个 function 遍历 dataframe,并将每一行的值作为参数。 For example:例如:

My pandas dataframe is as follows:我的pandas dataframe如下:

category  sales  met_sales
fruit     100    False
books     200    False
fruit     300    False

I have a dictionary: required_sales = {'fruit':150, 'books':200}我有一本字典: required_sales = {'fruit':150, 'books':200}

The output I want is this:我想要的 output 是这样的:

category  sales  met_sales
fruit     100    False
books     200    True
fruit     300    True

Is it correct to structure my function like that?像这样构造我的 function 是否正确?

def met_sales(df, dict):
    for row in df:
        if row.sales > required_sales[row.category]:
             #update met_sales column
             row.met_sales = True

Then, I can simply call met_sales(df,required_sales) to update my DataFrame.然后,我可以简单地调用met_sales(df,required_sales)来更新我的DataFrame。

Is this a good way of using self created functions to modify my DataFrame?这是使用自创函数修改我的 DataFrame 的好方法吗?

Use Series.map for dictionary and compare with column sales :Series.map用于字典并与列sales进行比较:

df['met_sales'] = df['sales'] >= df['category'].map(required_sales)
print (df)
  category  sales  met_sales
0    fruit    100      False
1    books    200       True
2    fruit    300       True

Detail :详情

print (df['category'].map(required_sales))
0    150
1    200
2    150
Name: category, dtype: int64

Function: Function:

Dont use dict as variable name as it is a reserved word for builtin python dict .不要使用dict作为变量名,因为它是内置 python dict的保留字。

def met_sales(df, d):
    df['met_sales'] = df['sales'] >= df['category'].map(d)
    return df

df1 = met_sales(df,required_sales)
print (df1)
  category  sales  met_sales
0    fruit    100      False
1    books    200       True
2    fruit    300       True

Notice :注意事项

It is necessary that all values of category are present in your dict, else missing values are returned for not existing keys:您的 dict 中必须存在category的所有值,否则会为不存在的键返回缺失值:

required_sales = {'fruit':150}

print (df['category'].map(required_sales))
0    150.0
1      NaN
2    150.0
Name: category, dtype: float64

暂无
暂无

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

相关问题 how to define a function that gets the number of columns in a pandas dataframe that takes the dataframe as the function's parameter - how to define a function that gets the number of columns in a pandas dataframe that takes the dataframe as the function's parameter 如果pandas中的列不存在于数据帧中,如何添加列 - how to add columns in pandas if its not present in dataframe 如何使用 function 比较 pandas dataframe 中列中的值? - How do I compare values in columns in a pandas dataframe using a function? 我应该为每个函数重新定义一个 Pandas 数据框吗? - Should I redefine a pandas dataframe with every function? 如何将函数应用于两列 Pandas 数据框 - How to apply a function to two columns of Pandas dataframe 如何将 function 应用于 pandas dataframe 的特定列? - How to apply a function to specific columns of a pandas dataframe? 如何将Pandas数据框填充为索引和列的函数 - How to populate Pandas dataframe as function of index and columns 如何将需要多个 arguments 的 function 应用到 pandas DataFrame - How to apply a function that takes multiple arguments to a pandas DataFrame 如何在 Pandas 数据帧列上并行应用函数? - How to apply a function on pandas dataframe columns in parallel? python-我如何从 pandas dataframe 中删除 2 列值的行(这些值应该是 2 个字符串的组合)? - python- How do i remove a rows from pandas dataframe by 2 columns value (The values should be a combination of 2 strings )?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM