使用Pandas时的范围达不到我的预期

时间:2019-03-09 00:44:34

标签: python pandas scope

我想将df输入到函数中,将输出调用另一个数据帧'df_fn',并使df不变。我该怎么办?

我的代码不执行任何操作,并且df_fn等于dfdf_fndf都被更改了。

使用df_fn[cols][df_fn.fuel_type != 'gas'] = np.nan

import pandas as pd
import numpy as np

df = pd.DataFrame({'n_wheels': [2, 4, 4],
                   'color': ['red', 'blue', 'red'],
                   'year': [2010, 1990, 1999],
                   'fuel_type': ['diesel', 'gas', 'electric']})
print('df = \n', df)
def fn(df_fn):

    cols = ['n_wheels', 'color', 'year']
#     df_fn.loc[df_fn.fuel_type != 'gas', cols] = np.nan
    df_fn[cols][df_fn.fuel_type != 'gas'] = np.nan


    return df_fn

new_df = fn(df)
print('df = \n', df)
print('new_df = \n', new_df)

输出:

df = 
    n_wheels color  year fuel_type
0         2   red  2010    diesel
1         4  blue  1990       gas
2         4   red  1999  electric

df = 
    n_wheels color  year fuel_type
0         2   red  2010    diesel
1         4  blue  1990       gas
2         4   red  1999  electric

new_df = 
    n_wheels color  year fuel_type
0         2   red  2010    diesel
1         4  blue  1990       gas
2         4   red  1999  electric

使用df_fn.loc[df_fn.fuel_type != 'gas', cols] = np.nan

print('df = \n', df)
def fn(df_fn):

    cols = ['n_wheels', 'color', 'year']
#     df_fn[cols][df_fn.fuel_type != 'gas'] = np.nan
    df_fn.loc[df_fn.fuel_type != 'gas', cols] = np.nan


    return df_fn

new_df = fn(df)
print('df = \n', df)
print('new_df = \n', new_df)

输出:

df = 
    n_wheels color  year fuel_type
0         2   red  2010    diesel
1         4  blue  1990       gas
2         4   red  1999  electric
df = 
    n_wheels color    year fuel_type
0       NaN   NaN     NaN    diesel
1       4.0  blue  1990.0       gas
2       NaN   NaN     NaN  electric
new_df = 
    n_wheels color    year fuel_type
0       NaN   NaN     NaN    diesel
1       4.0  blue  1990.0       gas
2       NaN   NaN     NaN  electric

1 个答案:

答案 0 :(得分:2)

您需要设置原始df的副本

print('df = \n', df)
def fn(df_fn):
    cols = ['n_wheels', 'color', 'year']
#     df_fn[cols][df_fn.fuel_type != 'gas'] = np.nan
    df_fn.loc[df_fn.fuel_type != 'gas', cols] = np.nan
    return df_fn
df1=df.copy()#I change here add copy 
new_df = fn(df1)
print('df = \n', df)
print('new_df = \n', new_df)
df = 
    n_wheels color  year fuel_type
0         2   red  2010    diesel
1         4  blue  1990       gas
2         4   red  1999  electric
df = 
    n_wheels color  year fuel_type
0         2   red  2010    diesel
1         4  blue  1990       gas
2         4   red  1999  electric
new_df = 
    n_wheels color    year fuel_type
0       NaN   NaN     NaN    diesel
1       4.0  blue  1990.0       gas
2       NaN   NaN     NaN  electric
相关问题