【发布时间】:2018-07-03 00:12:06
【问题描述】:
我有一个列(TESTFILE),它有 x 值(在这个例子中 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 语句。请帮忙
【问题讨论】:
-
如果这是一个格式问题,也许只需在必要的地方添加一个新行
\n,例如(testfile.iloc[i,0] + '\n')*n ?
标签: python-3.x pandas append