我有一个以下形式的数据框:
time value label
0 2020-01-01 -0.556014 high
1 2020-01-02 0.185451 high
2 2020-01-03 -0.401111 medium
3 2020-01-04 0.436111 medium
4 2020-01-05 0.412933 high
5 2020-01-06 0.636421 high
6 2020-01-07 1.168237 high
7 2020-01-08 1.205073 high
8 2020-01-09 0.798674 high
9 2020-01-10 0.174116 high
我想填充一个数据帧列表,其中每个数据帧都是在列中的字符串时构建的label
变化。所以第一个数据框是:
time value label
0 2020-01-01 -0.556014 high
1 2020-01-02 0.185451 high
第二个数据框是:
time value label
2 2020-01-03 -0.401111 medium
3 2020-01-04 0.436111 medium
等等。所需的列表是[df, df, ...]
。如果您认为字典是更合适的容器,我根本不介意。
有一个类似的帖子叫如果列值的顺序发生变化,则拆分数据框 pandas,但这仅处理数值的变化。
我已经做了一些尝试,但在比较行值时不断遇到索引问题label
与之前的值。所以任何建议都会很棒!
这是一个可重现的片段:
# imports
import plotly.express as px
import plotly.graph_objects as go
import pandas as pd
import numpy as np
import random
# settings
observations = 100
np.random.seed(5)
value = np.random.uniform(low=-1, high=1, size=observations).tolist()
time = [t for t in pd.date_range('2020', freq='D', periods=observations).format()]
df=pd.DataFrame({'time': time,
'value':value})
df['value']=df['value'].cumsum()
def classify(e):
if e > 0.75: return 'high'
if e > 0.25: return 'medium'
if e >= 0: return 'low'
df['label1'] = [(elem-df['value'].min())/(df['value'].max()-df['value'].min()) for elem in df['value']]
df['label'] = [classify(elem) for elem in df['label1']]
df = df.drop('label1', 1)
df