我有一个这样的数据帧,
d = {'ID': ["A", "A", "B", "B", "C", "C", "D", "D", "E", "E", "F", "F"],
'value': [23, 23, 52, 52, 36, 36, 46, 46, 9, 9, 110, 110]}
df = pd.DataFrame(data=d)
ID value
0 A 23
1 A 23
2 B 52
3 B 52
4 C 36
5 C 36
6 D 46
7 D 46
8 E 9
9 E 9
10 F 110
11 F 110
基本上,我复制原始数据集(n 行(。我想得到的数据帧似乎是这样的,
ID value
0 A 23
1 B 23
2 B 52
3 C 52
4 C 36
5 D 36
6 D 46
7 E 46
8 E 9
9 F 9
将值列向下移动一个单位,并保留所有值对。所以,我失去了A的第一个,F的最后一个和最后两个值110。最后,我有 2n-2 行。
我认为需要:
df = df.set_index('ID').shift().iloc[1:-1].reset_index()
print (df)
ID value
0 A 23.0
1 B 23.0
2 B 52.0
3 C 52.0
4 C 36.0
5 D 36.0
6 D 46.0
7 E 46.0
8 E 9.0
9 F 9.0
我认为这将解决您的问题
import pandas as pd
d = {'ID': ["A", "A", "B", "B", "C", "C", "D", "D", "E", "E","F","F"],
'value': [23, 23, 52, 52, 36, 36, 46, 46, 9, 9, 110, 110]}
df = pd.DataFrame(data=d)
df['value']=df['value'].shift(1)
df2=df[1:11] #here 11 is n-1,it depends on number of rows
print(df2)
因此,
如果您只想将ID
移位 -1 并排除最后 2 行:
df['ID'] = df['ID'].shift(-1)
result = df[:-2]