将 Pandas 数据帧对象传递给函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36872678/
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 01:07:42  来源:igfitidea点击:

Passing a pandas dataframe object into a function

pythonfunctionpandasdataframe

提问by Obabs

I have dataframe objects that I would like to to pass into a function.

我有我想传递给函数的数据帧对象。

x = pd.Dataframe()

def function(z):
    "code"
    return result

result = function(x)

I'm new to python, can someone please steer me in the right direction.

我是python的新手,有人可以引导我朝着正确的方向前进。

回答by pmaniyan

Below I am showing a simple function that would have the input parameter as a DataFrame object, and it would check if one of the columns has the String "Some", if so, it returns the Boolean results.

下面我展示了一个简单的函数,它将输入参数作为一个 DataFrame 对象,它会检查其中一列是否有字符串“Some”,如果有,它返回布尔结果。

Check if this helps.

检查这是否有帮助。

x = pd.DataFrame([[1,'Some Text'],[2,'New Text']],columns=('SINO','String_Column'))

def function(z):
    l_local_df = z['String_Column'].str.contains('Some')
    return l_local_df

result = function(x)
print result