Python 使用熊猫时,范围不是我所期望的
我想将Python 使用熊猫时,范围不是我所期望的,python,pandas,scope,Python,Pandas,Scope,我想将df输入到一个函数中,将输出调用另一个数据帧'df_fn',并使df保持不变。我该怎么做 我的代码要么什么都不做,要么df_fn等于df,要么df_fn和df都被修改 使用df\u fn[cols][df\u fn.fuel\u type!=“gas”]=np.nan import pandas as pd import numpy as np df = pd.DataFrame({'n_wheels': [2, 4, 4], 'color': ['
df
输入到一个函数中,将输出调用另一个数据帧'df_fn',并使df
保持不变。我该怎么做
我的代码要么什么都不做,要么df_fn
等于df
,要么df_fn
和df
都被修改
使用df\u fn[cols][df\u fn.fuel\u 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)
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 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 =
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
使用df_fn.loc[df_fn.fuel_type!='gas',cols]=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)
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 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 =
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
您需要设置原始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
哦,是的,这很有效。你能解释一下为什么我的两个版本不起作用吗?我不应该因为作用域是如何工作的而不需要复制吗?@vivian first one如果你在函数之外运行它,就会返回一个警告
df[cols][df.fuel\u type!='gas']=np.nan
try,第二个会在没有复制的情况下重写原始df