1.
data = [{"a": 1, "b": 2, "c": 3},
{"a": 11, "b": 22, "c": 33},
{"a": 111, "b": 222, "c": 333}]
df = pd.DataFrame(data)
df
df['stat'] = df['a'].map(lambda x: 'big' if x > 100 else 'small')
df
2.
def func(i):
return 'big' if i > 100 else 'small'
df['b_stat'] = df['b'].map(func)
df
3. 分组聚合
data = [{"a": 1, "b": 2, "c": 3, "e": 4},
{"a": 11, "b": 22, "c": 33,"e": 4},
{"a": 111, "b": 222, "c": 333, "e": 41},
{"a": 1, "b": 21, "c": 3, "e": 41},
{"a": 11, "b": 22, "c": 33},
{"a": 111, "b": 23, "c": 333},
{"a": 1, "b": 2, "c": 3, "e": 4},
{"a": 11, "b": 22, "c": 33},
{"a": 111, "b": 24, "c": 333},
]
import pandas as pd
df = pd.DataFrame(data)
df
df = df.groupby(['a', 'b']).agg({'c': 'count', 'e': 'sum'}).reset_index(drop=False)
df