我有一个具有x值的列(TESTFILE)(在此示例中x = 4,它是否接近50:
我希望输出为:
代码:
out1 = []
for i in range(len(testfile)):
n = len(testfile) - 1
if i ==0:
filepath = (testfile.iloc[i,0])*n
out1.append(filepath)
elif i ==1:
filepath = (testfile.iloc[i,0])* (n-1)
out1.append(filepath)
else:
filepath = (testfile.iloc[i,0])* (n-2)
out1.append(filepath)
print (filepath)
目前的输出是:
如何获得所需的输出?如果x = 50,我应该如何更改代码?不能写更多if语句。请帮忙
答案 0 :(得分:4)
使用repeat
s.repeat(s.index.values[::-1]).reset_index(drop=True)
Out[1183]:
0 hello
1 hello
2 hello
3 good
4 good
5 job
Name: Val, dtype: object
数据输入
s
Out[1182]:
0 hello
1 good
2 job
3 today
Name: Val, dtype: object