1. 基础数据准备
import pandas as pd
data = [{"a": 1, "b": ' djidn. '},
{"a": 11, "b": 22.123456},
{"a": 111, "b": ''},
{"a": 1111},
{"a": '1111'}]
df = pd.DataFrame(data)
df
2. 操作方法
def func(num):
if num != num:
return ''
elif isinstance(num, float):
return str(round(num, 2))
elif isinstance(num, int):
return str(num)
elif isinstance(num, str):
return num.strip()
else:
return num
3. 对单列操作
df['a'] = df['a'].apply(func)
df['b'] = df['b'].apply(func)
3. 对整个DF的操作
df = df.apply(lambda x:x.map(func))
或
df = df.applymap(func)