Python 3.x 如何使用python连续减少值在列中出现的次数?
我有一个列(TESTFILE),它有x个值(在本例中,x=4,可以接近50:Python 3.x 如何使用python连续减少值在列中出现的次数?,python-3.x,pandas,append,Python 3.x,Pandas,Append,我有一个列(TESTFILE),它有x个值(在本例中,x=4,可以接近50: 你好 好 工作 今天 我希望输出为: 你好 你好 你好 好 好 工作 代码: 目前的输出是: 你好 好极了 工作 如何获得所需的输出?如果x=50,我应该如何更改代码?无法写入更多的if语句。请使用repeat s.repeat(s.index.values[::-1]).reset_index(drop=True) Out[1183]: 0 hello 1 hello 2 hello 3
如何获得所需的输出?如果x=50,我应该如何更改代码?无法写入更多的if语句。请使用
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
使用
重复
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
如果这是一个格式化问题,可能只需要在必要的地方添加一行\n
,例如(testfile.iloc[i,0]+'\n')*n?如果这是一个格式化问题,可能只需要在必要的地方添加一行\n
,例如(testfile.iloc[i,0]+'\n')*n?这是genius!这是genius!