I have a large csv, with a column that contains numbers preceeding with a zero and ending with 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 ..
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..
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
You need to read it in as a str
then you can slice it:
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!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.