python - 从函数修改熊猫数据框
问题描述
我发现自己试图一次又一次地用相同的操作修改几个数据帧。我想将所有修改放在一个函数中,然后使用数据框名称调用该函数并完成所有转换。
这是我现在尝试应用的代码和所有转换。当我运行它时,什么都没有发生,并且数据框保持原始状态。
#create a preprocessing formula so the process can be applied to any dataset (traning and validation and competition)
def preprocessing(df):
#inspect dataframe
df.head()
#check data types in dataframe
np.unique(df.dtypes).tolist()
#inspect shape before removing duplicates
df.shape
#drop duplicates
df = df.drop_duplicates()
#inspect shape again to see change
df.shape
#calculate rows that have a mean of 100 to remove them later
mean100_rows = [i for i in range(len(df)) if df.iloc[i,0:520].values.mean() == 100 ]
#calculate columns that have a mean of 100 to remove them later
mean100_cols = [i for i in np.arange(0,520,1) if df.iloc[:,i].values.mean() == 100 ]
#calculate columns labels that have a mean of 100 to remove them later
col_labels = [df.columns[i] for i in mean100_cols]
#delete rows with mean 100
df.drop(index = mean100_rows, axis=0, inplace=True)
#delete columns with mean 100
df.drop(columns=col_labels, axis=1, inplace=True)
#export columns that have been removed
pd.Series(col_labels).to_csv('remove_cols.csv')
#head
df.head()
#check size again
df.shape
解决方案
在 Python 中,对象通过引用传递给函数。
执行以下行时
df = df.drop_duplicates()
您基本上为函数参数分配了新的引用,但函数之外的对象并没有改变。
我建议更改函数,使其返回 df 对象,然后将其返回值分配给函数外部的 df 对象。
推荐阅读
- python - 如何在 python fu 中运行笼子变换
- php - Wordpress 从一台服务器迁移到另一台服务器(找不到页面)
- python - 我正在尝试使用 django 表单创建注册表单。但是当我输入数据时,它会在 /register 处返回 ValueError?
- variables - 如何在多个 Arduino 之间使用相同的变量?
- python-3.x - 我如何在 python 3 中使用 facebook API(V3.3) 搜索 facebook 用户
- python - 如何将混合时间和日期时间值的列转换为日期时间(或时间)?
- java - 如何使用 spring-kafka 框架将对象列表发送到 kafka 主题?
- c++ - 如何将向量推入队列
> 使用循环? - java - 如果有的话,用松耦合或高耦合改进我的代码?
- database - 运行一个完成它的工作的 Yaml 脚本,但它一直给我一个语法错误