import pandas as pd
import numpy as np
d = {"id":[15594815,15805254,15656148],"score":[62.118782,13.003589,997.3572]}
df_map = pd.DataFrame(data=d)
df_map.head()
def score2label(x):
if x>500:
return 1
else :
return 0
#df_map['score1'] = df_map['score'].map(lambda x: 1 if x>500 else 0)
df_map['score1'] = df_map['score'].map(score2label)
df_map.head()
def score2label1(x, y):
if x>500:
return 1 + y
else :
return 0 + y
# apply() applymap() 是 pandas 函数, apply()作用于一列,通常为统计,applymap()为所有
df_map['score2'] = df_map['score'].apply(score2label1, y=3)
df_map.head()
df_map.apply(np.sum, axis=0)
df_map.apply(np.sum, axis=1)
df_map = df_map.applymap(lambda x: '%.2f' % x)
df_map.head()