简体   繁体   English

如何修改函数以根据 Python Pandas 中的值返回 2 个 DataFrame?

[英]How to modify function so as to return 2 DataFrame depending on values in Python Pandas?

I have function in Python Pandas like below:我在 Python Pandas 中有如下功能:

def my_func(df, col: str):
    if pd.isna(df[col]):
          return False

To use my function I need: df_resul = my_func(df = my_df, col = "col1")要使用我的功能,我需要: df_resul = my_func(df = my_df, col = "col1")

And Data Frame like below where col1 is string data type:和下面的数据框,其中 col1 是字符串数据类型:

col1
--------
NaN
ABC
NaN

How can I modify my function, so as to as a result have 2 different DataFrames:如何修改我的函数,以便有 2 个不同的 DataFrame:

  1. Where in col1 is NaN col1 中的哪里是 NaN
  2. Where in col1 is value other than NaN col1 中的值不是 NaN

So to use my function I need: df_nan, df_not_nan = my_func(df = my_df, col = "col1") where df_nan will return df where in col1 is nan and df_not_nan will return df where in col is value other than nan.因此,要使用我的函数,我需要: df_nan, df_not_nan = my_func(df = my_df, col = "col1")其中 df_nan 将返回 df,其中 col1 是 nan,而 df_not_nan 将返回 df,其中 col 是 nan 以外的值。

df_nan: df_nan:

col1
------
NaN
NaN

df_not_nan: df_not_nan:

col1
-----
ABC

How can I modify my function in Python Pandas ?如何在 Python Pandas 中修改我的函数?

Use boolean indexing with ~ fo rinvert mask, here for select non missing values rows:boolean indexing~用于 rinvert 掩码一起使用,此处用于选择非缺失值行:

print (my_df)
  col1  a
0  NaN  1
1  ABC  2
2  NaN  3

def my_func(df, col: str):
    m = df[col].isna()
    return df[m], df[~m]
 
df_nan, df_not_nan = my_func(df = my_df, col = "col1")
print (df_nan)
  col1  a
0  NaN  1
2  NaN  3

print (df_not_nan)
  col1  a
1  ABC  2

If need test if exist at least one missing value is necesary add Series.any for avoid error如果需要测试是否存在至少一个缺失值,则需要添加Series.any以避免错误

ValueError: The truth value of a Series is ambiguous. ValueError:Series 的真值不明确。 Use a.empty, a.bool(), a.item(), a.any() or a.all()使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()

def my_func1(df, col: str):
    if pd.isna(df[col]).any():
        return  'exist at least one missing values'
    else:
        return 'no missing values'

 
out = my_func1(df = my_df, col = "col1")
print (out)
exist at least one missing values

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

相关问题 如何修改循环以便从 Pandas Python 中的 DataFrame 中的列中的值中获取 NaN 值? - How to modify loop so as to take NaN values from values in columns in DataFrame in Pandas Python? 如何在函数中修改pandas DataFrame,以便调用者可以看到更改? - How to modify a pandas DataFrame in a function so that changes are seen by the caller? 如何有条件地修改数据框列中的字符串值-Python / Pandas - How to conditionally modify string values in dataframe column - Python/Pandas 根据 i-1 处的值修改第 i 行的 pandas dataframe - Modify pandas dataframe at row i depending on values at i-1 修改函数以返回具有指定值的数据帧 - Modify function to return dataframe with specified values 在 python ZC1C425268E68385D1AB5074C17A94F1Z4 中修改 Pandas dataframe - Modify a Pandas dataframe inside a python function 根据列数据类型,返回 Pandas 数据框列的均值或模式的 Python 函数 - Python function to return mean or mode of a pandas dataframe column, depending on the column data type 修改python pandas数据框 - modify python pandas dataframe Pandas 根据函数返回单独的 DataFrame 值 - Pandas Return Separate DataFrame Values Based on Function 如何修改数据框以便在 Python Pandas 列中的其他字符之间取值? - How to modify Data Frame so as to take values between some other character in column in Python Pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM