[Answered ]-Keep the original value from pandas dataframe

2👍

Use parameter dtype=str for cast all values to string in read_csv:

pd.read_csv('/output.csv', header=None, names=['c1','c2'], dtype=str)

Sample:

import pandas as pd
from pandas.compat import StringIO

temp=u"""A,-0.1234540756893158
B,0.123450496711731
C,0.12345994493484497
D,-0.12345484461784363
E,12344656.0
F,-1234648.0
G,12342316.0
H,12552.37109375
I,16247.228515625
J,-12.123796875
K,1081104201
L,123"""
#after testing replace 'StringIO(temp)' to 'filename.csv'
df = pd.read_csv(StringIO(temp), header=None, names=['c1','c2'], dtype=str)
print (df)
   c1                    c2
0   A   -0.1234540756893158
1   B     0.123450496711731
2   C   0.12345994493484497
3   D  -0.12345484461784363
4   E            12344656.0
5   F            -1234648.0
6   G            12342316.0
7   H        12552.37109375
8   I       16247.228515625
9   J         -12.123796875
10  K            1081104201
11  L                   123

print (type(df.loc[0, 'c2']))
<class 'str'>

Leave a comment