columnA columnB
0 a1|a2|a3|a4|a5|a6|a7|a8|a9|a10 0
1 b1|b2|||||||| 8
2 c1|c2|c3||||||| 7
3 d1|d2|||||||| 8
4 e1|e2|||||||| 8
5 f1|f2|f3|f4|f5|f6|f7||| 3
6 g1|g2|g3|g4|||||| 6
i want the first line to remove 0 characters to the right, the second line to remove 8 characters to the right, The resulting data will have the following form
columnA columnB
0 a1|a2|a3|a4|a5|a6|a7|a8|a9|a10 0
1 b1|b2 8
2 c1|c2|c3 7
3 d1|d2 8
4 e1|e2 8
5 f1|f2|f3|f4|f5|f6|f7 3
6 g1|g2|g3|g4 6
Thank you very much everyone. I am a newbie and my English is not very good. Hope everyone can help me
CodePudding user response:
You can use Pandas' string methods on column 'columnA'
rstrip
df.columnA.str.rstrip('|')
0 a1|a2|a3|a4|a5|a6|a7|a8|a9|a10
1 b1|b2
2 c1|c2|c3
3 d1|d2
4 e1|e2
5 f1|f2|f3|f4|f5|f6|f7
6 g1|g2|g3|g4
Name: columnA, dtype: object
Not altering df
df.assign(columnA=df.columnA.str.rstrip('|'))
columnA columnB
0 a1|a2|a3|a4|a5|a6|a7|a8|a9|a10 0
1 b1|b2 8
2 c1|c2|c3 7
3 d1|d2 8
4 e1|e2 8
5 f1|f2|f3|f4|f5|f6|f7 3
6 g1|g2|g3|g4 6
overwriting df
df['columnA'] = df.columnA.str.rstrip('|')
df
columnA columnB
0 a1|a2|a3|a4|a5|a6|a7|a8|a9|a10 0
1 b1|b2 8
2 c1|c2|c3 7
3 d1|d2 8
4 e1|e2 8
5 f1|f2|f3|f4|f5|f6|f7 3
6 g1|g2|g3|g4 6