【问题标题】:How to pass a DataFrame column as an argument in a function?如何将 DataFrame 列作为函数中的参数传递?
【发布时间】:2020-11-14 02:02:12
【问题描述】:

enter image description here 检查特定值的分布,例如给定列中天气正好多云的次数。随意检查其他值。你可以通过调用clear函数来检查它的参数。

#Importing the modules
import pandas as pd
import numpy as np
from scipy.stats import mode 

weather = pd.read_csv(path, sep = ",", delimiter = None)
#code to check distribution of variable
def clear(df,col,val):
    """ Check distribution of variable
    df,col,val
    This function accepts a dataframe,column(feature) and value which returns count of the value,
    containing the value counts of a variable(value_counts)

Keyword arguments:
df - Pandas dataframe
col - Feature of the datagrame
val - value of the feature

Returns:
value_counts - Value count of the feature 
"""
        
value_counts = df[(df.col == val)]     # filtering dataframe
print(len(value_counts))

clear(weather,weather.Weather,'Cloudy') #调用函数clear

【问题讨论】:

    标签: python pandas function parameter-passing filtering


    【解决方案1】:

    也许只是传递列的名称 - 而不是整个列:

    def clear(df, column_name, column_value):
        value_counts = df.loc[(df[column_name] == column_value)]     # filtering dataframe
        return len(value_counts)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-21
      • 2020-03-22
      • 2022-01-13
      • 2014-06-29
      • 1970-01-01
      • 2014-12-03
      • 2021-01-14
      • 2022-01-02
      相关资源
      最近更新 更多