SUBIN K SOMAN
SUBIN K SOMAN

Reputation: 406

how to avoid removing 0 from msb in python panda dataframe

i have data in column like 0123456789 after reading from a file it will get like 123456789 where column name is msisdn

how to fix this issue

am using the pandas script as follows

#!/usr/bin/env python 
import gc
import pandas
csv1 = pandas.read_csv('/home/subin/Desktop/a.txt')
csv2 = pandas.read_csv('/home/subin/Desktop/b.txt')
merged = pandas.merge(csv1, csv2,left_on=['MSISDN'],right_on=['MSISDN'],how='left',suffixes=('#x', '#y'), sort=True).fillna('0')
merged.to_csv("/home/subin/Desktop/amergeb_out.txt", index=False, float_format='%.0f')

Upvotes: 2

Views: 84

Answers (2)

SUBIN K SOMAN
SUBIN K SOMAN

Reputation: 406

csv1 = pandas.read_csv('/home/subin/Desktop/a.txt',dtype=str)
csv2 = pandas.read_csv('/home/subin/Desktop/b.txt',dtype={'MSISDN': str})
merged = pandas.merge(csv1, csv2,left_on=['MSISDN'],right_on=['MSISDN'],how='left',suffixes=('#x', '#y'), sort=True).fillna('0')
merged.to_csv("/home/subin/Desktop/amergeb_out.txt", index=False, float_format='%.0f')

Upvotes: 0

jezrael
jezrael

Reputation: 863301

You can cast column msisdn to string by parameter dtype in read_csv:

temp=u"""msisdn
0123456789
0123456789"""
#after testing replace io.StringIO(temp) to filename
df = pd.read_csv(io.StringIO(temp), dtype={'msisdn': str})
print (df)
       msisdn
0  0123456789
1  0123456789

Upvotes: 1

Related Questions