我想将df
输入到函数中,将输出调用另一个数据帧'df_fn',并使df
不变。我该怎么办?
我的代码不执行任何操作,并且df_fn
等于df
或df_fn
和df
都被更改了。
使用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
答案 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