Python数据清洗基本流程
# -*- coding: utf-8 -*- """ Created on Wed Jul 4 18:40:55 2018 @author: zhen """ import pandas as pd import numpy as np # 创建空的df,保存测试数据 test_df = pd.DataFrame({'K1':['C1','C1','C2','C3','C4','C2','C1'],'K2':['A','A','B','C','D',np.NaN,np.NaN]}) # 按K1列进行分组,组内进行unique操作(去除重复元素,返回元组或列表) test_df_unique = pd.DataFrame(test_df. groupby (['K1'])['K2']. agg('unique') ) # 自定义函数判断元组中是否含有nan def has_nan(list): flag = False for x in list: if x is np.NaN: flag = True break return flag # 自定义函数判断元组中是否不含有nan def no_nan(list): flag = True for x in list: if x is np.NaN: flag