以SharedDatadata为例写一个处理缺失值的代码将处理好的文件放在SharedData2data中输出
以下是一个处理缺失值的Python代码示例,使用了pandas库:
import pandas as pd
# 读取原始数据文件
df = pd.read_csv('SharedData.data')
# 查看缺失值情况
print(df.isnull().sum())
# 用均值填补缺失值
df.fillna(df.mean(), inplace=True)
# 再次查看缺失值情况
print(df.isnull().sum())
# 将处理好的数据保存到新文件
df.to_csv('SharedData2.data', index=False)
该代码首先使用pandas库的read_csv函数读取原始数据文件,然后使用isnull和sum方法查看数据中各列的缺失值数量。接着使用fillna方法将缺失值用均值填补,再次查看缺失值情况,确认所有缺失值都已被填补。最后使用to_csv方法将处理好的数据保存到新文件SharedData2.data中。
原文地址: https://www.cveoy.top/t/topic/frvD 著作权归作者所有。请勿转载和采集!