DataFrame 列运算

import pandas as pd
import StringIO

table_buffer = StringIO.StringIO('''a b
2007-01-08  0.786667        270
2007-01-09  0.853333        280
2007-01-10  0.866667        282
2007-01-11  0.880000        277
2007-01-12  0.880000        266
2007-01-15  0.866667        279''')
df = pd.read_table(table_buffer, sep='\s+')

def calc_c(row):
    if row.a > 0.5 and row.a < 0.9:
        return row.b
    else:
        return None

df['c'] = df.apply(calc_c, axis=1)

# 对于简单的条件可以用匿名函数
df['c'] = df.apply(lambda row: row.b if row.a > 0.5 and row.a < 0.9 else None, axis=1)

 

refer to:

https://www.zhihu.com/question/54631460

 

posted @ 2019-01-07 15:02  清源居士  阅读(2272)  评论(0编辑  收藏  举报