简体   繁体   English

Python Pandas从列中删除最后一个字符串/符号

[英]Python pandas remove last string/symbol from a column

I have a large csv, with a column that contains numbers preceeding with a zero and ending with a . 我有一个很大的csv,其中一列包含以零开头和以a结尾的数字。

It looks like .. 看起来像 ..

TC_NUM
0101.0001.
0101.0002.
0101.0003.

I want it to look like.. 我希望它看起来像..

TC_NUM

    0101.0001
    0101.0002
    0101.0003

My code: 我的代码:

df3['TC_NUM'] = df3['TC_NUM'].astype(str).str[:-1]

and wrong output of my code.. 和我的代码输出错误。

TC_NUM
101.0001
101.0002
101.0003
101.0004
101.0005

Using edchums fix .. 使用edchums修复..

df4 = pd.read_csv('output2.csv', dtype=object, index_col=0)
print df4.head()
df4['TC_NUM'] = df4['TC_NUM'].str[:-1]

It prints correctly.. 它可以正确打印。

0                 dialog_testcase_0101.0001_greeting.xml       0101.0001
1                 dialog_testcase_0101.0002_greeting.xml       0101.0002
2                 dialog_testcase_0101.0003_greeting.xml       0101.0003
3                 dialog_testcase_0101.0004_greeting.xml       0101.0004
4                 dialog_testcase_0101.0005_greeting.xml       0101.0005

but using this 但是用这个

df4['TC_NUM'] = df4['TC_NUM'].str[:-1]
print df4.head
df4.to_csv('output2.csv', dtype=object,index_col=0)

The resulting csv output is.. 最终的csv输出是..

0   dialog_testcase_0101.0001_greeting.xml  101.0001
1   dialog_testcase_0101.0002_greeting.xml  101.0002
2   dialog_testcase_0101.0003_greeting.xml  101.0003
3   dialog_testcase_0101.0004_greeting.xml  101.0004

Hence missing the beginning 0 因此缺少开头0

You need to read it in as a str then you can slice it: 您需要将其读为str然后可以对其进行切片:

In [11]:
t="""TC_NUM
0101.0001.
0101.0002.
0101.0003."""
df = pd.read_csv(io.StringIO(t), dtype=object)
df

Out[11]:
       TC_NUM
0  0101.0001.
1  0101.0002.
2  0101.0003.

In [13]:
df['TC_NUM'] = df['TC_NUM'].str[:-1]
df

Out[13]:
      TC_NUM
0  0101.0001
1  0101.0002
2  0101.0003

I eventually figured it out after much coding, and keyboard bashing. 经过大量的编码和键盘打击,我终于弄明白了。 The code was correct as the output printed, but open office naturally removes the leading zeros from columns. 该代码在打印输出时是正确的,但开放办公室自然会删除列中的前导零。 Solution was to put the numbers in '' so open office would leave it intact. 解决方案是将数字放在''中,这样开放式办公室就可以完好无损。 Thank you all for the help! 谢谢大家的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM