1 、某列数据加1
from decimal import Decimal
def add_df(x):
x = 1 + Decimal(x)
return x
# 对roc列+1 并赋值给列命为roc+1的列
df["rco+1"]= df['roc'].map(add_df)
df_line = np.cumprod(df['rco+1'][:]).tolist()
2、某列数据判断
import pandas as pd
df = pd.DataFrame([[1, 'A'], [3, 'B'], [3, 'C']], columns=list('AB'))
def add_extra_kwargs(column, **kwargs):
return kwargs[column]
#方法 1
df['A'] = df.B.apply(add_extra_kwargs, A=0, B=10,C=5)
df['C'] = df["B"].apply(add_extra_kwargs, A=0, B=10,C=5)
print(df)
def add_extra(field, extra):
if field != "A":
return extra
else:
return 0
#方法 2
df['add_extra'] = df.B.apply(add_extra, args=(5,))
print(df)
df['extra'] = df.B.apply(add_extra, extra=5)
print(df)
3、某列数据时间格式转换
df['times '] = df['times '].apply(lambda x:x.strftime('%Y-%m-%d %H:%M:%S'))
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)