Python 如何将具有递减值的多行添加到序列中
我有一系列的数字: 5138 22498 42955 5138 22498 42955 我想在上述数字的基础上增加3个数字(每个数字减少1个): 5135 5136 5137 5138 22495 22496 22497 22498 42952 42953 42954 42955 5135 5136 5137 5138 22495 22496 22497 22498 42952 42953 42954 42955Python 如何将具有递减值的多行添加到序列中,python,pandas,Python,Pandas,我有一系列的数字: 5138 22498 42955 5138 22498 42955 我想在上述数字的基础上增加3个数字(每个数字减少1个): 5135 5136 5137 5138 22495 22496 22497 22498 42952 42953 42954 42955 5135 5136 5137 5138 22495 22496 22497 22498 42952 42953 42954 42955 怎么做?谢谢。使用列表理解,将范围创建的新值展平: s = pd.Series([
怎么做?谢谢。使用列表理解,将
范围创建的新值展平:
s = pd.Series([5138,22498,42955])
N = 3
a = pd.Series([y for x in s for y in range(x-N, x+1)])
print (a)
0 5135
1 5136
2 5137
3 5138
4 22495
5 22496
6 22497
7 22498
8 42952
9 42953
10 42954
11 42955
dtype: int64
或者可以创建范围并展平,最后一个用于默认索引:
N = 3
a = s.apply(lambda x: range(x-N, x+1)).explode().reset_index(drop=True)
我在python上这样做:
numbers = [5138, 22498, 42955]
for number in numbers:
for i in reversed(range(number, number-4, -1)):
print(i)
希望这有帮助:)