当另一列增加/减少时添加状态列

时间:2020-01-14 12:59:20

标签: python pandas dataframe

当另一列正在增加/减少或与以下内容保持不变时,我想在数据框中添加一列: 1->递增,0->相同,-1->递减

如果df['battery'] = [1,2,3,4,7,9,3,3,3,] 我希望状态为df['state'] = [1,1,1,1,1,-1,0,0]

4 个答案:

答案 0 :(得分:1)

这应该可以解决问题!

a = [1,2,3,4,7,9,3,3,3]
b = []

for x in range(len(a)-1):
  b.append((a[x+1] > a[x]) - (a[x+1] < a[x]))

print(b)

答案 1 :(得分:1)

您可以使用pd.Series.diff方法获取连续值之间的差,然后使用boolean indexing分配必要的状态值:

import pandas as pd

df = pd.DataFrame()
df['battery'] = [1,2,3,4,7,9,3,3,3]

diff = df['battery'].diff()
df.loc[diff > 0, 'state'] = 1
df.loc[diff == 0, 'state'] = 0
df.loc[diff < 0, 'state'] = -1

print(df)
#    battery  state
# 0        1    NaN
# 1        2    1.0
# 2        3    1.0
# 3        4    1.0
# 4        7    1.0
# 5        9    1.0
# 6        3   -1.0
# 7        3    0.0
# 8        3    0.0

或者,也可以使用np.select

import numpy as np

diff = df['battery'].diff()
df['state'] = np.select([diff < 0, diff > 0], [-1, 1], 0)
# Be careful, default 0 will replace the first NaN as well.

print(df)
#    battery  state
# 0        1      0
# 1        2      1
# 2        3      1
# 3        4      1
# 4        7      1
# 5        9      1
# 6        3     -1
# 7        3      0
# 8        3      0

答案 2 :(得分:1)

这是您的数据框:

>>> import pandas as pd
>>> data = [[[1,2,3,4,7,9,3,3,3]]]
>>> df = pd.DataFrame(data, columns = ['battery'])
>>> df
                       battery
0  [1, 2, 3, 4, 7, 9, 3, 3, 3]

最后使用applylambda函数来生成所需的结果:

>>> df['state'] = df.apply(lambda row: [1 if t - s > 0 else -1 if t-s < 0 else 0 for s, t in zip(row['battery'], row['battery'][1:])], axis=1)
>>> df
                       battery                      state
0  [1, 2, 3, 4, 7, 9, 3, 3, 3]  [1, 1, 1, 1, 1, -1, 0, 0]

或者,如果您想要列表中每个元素之间的确切区别,则可以使用以下内容:

>>> df['state'] = df.apply(lambda row: [t - s for s, t in zip(row['battery'], row['battery'][1:])], axis=1)
>>> df
                       battery                      state
0  [1, 2, 3, 4, 7, 9, 3, 3, 3]  [1, 1, 1, 3, 2, -6, 0, 0]

答案 3 :(得分:1)

尝试pd.np.sign

pd.np.sign(df.battery.diff().fillna(1))
0    1.0
1    1.0
2    1.0
3    1.0
4    1.0
5    1.0
6   -1.0
7    0.0
8    0.0
Name: battery, dtype: float64