基于python的大数据分析之修改记录

修改记录是常有的事情,比如数据中有的需要整体替换,有的需要特别修改.

  1. 整体替换
    整列、整行的替换很简单,例如执行语句:df[‘a列’] = score,该语句中score是将被填进去的数据列(可以是列表或者Series)

  2. 个别修改
    有以下几种情况

# -*- coding: utf-8 -*-
"""
Created on Wed Dec 25 14:42:27 2019

@author: EricRay

Description: 基于python的大数据分析之修改记录
"""

import pandas as pd
#from pandas import read_excel

df = pd.read_excel(r'E:\python\hello\files\i_nuc.xls',sheet_name='Sheet3')

print(df.head(),'\n')

#单值替换: df.replace('b','a') a替换b
print('单值替换:\n',df.replace('作弊',0))

#指定列单值替换 ,0替换体育列中的作弊,军训列中的缺考
print("指定列单值替换:\n",df.replace({'体育':'作弊','军训':'缺考'},0))

"""
多值替换:
df.replace(['a','b'],['c','d']) c替换a,d替换b
或者写成
df.replace({'a':'c','b':'d'})
df.replace({'a','b'},{'c','d'})
"""
print("多值替换:\n",df.replace({'成龙':'陈龙','周怡':'周依依'}))


你可能感兴趣的:(基于python的大数据分析)