Search code examples
pythonpandascsvbinaryone-hot-encoding

Trouble writing binary numbers in a file using Pandas


My code consists of removing the redundancy from the code as a form of backup. But when writing again in the file it removes the zeroes to the left of the 1. Like in the image below

https://imgur.com/a/OU07DzX

mydataset2 = pd.read_csv('fieldstatebackup.binetflow')

mydataset2.drop_duplicates(['State2','State'], keep='first', inplace=True)
mydataset2.to_csv('fieldstatebackup.binetflow', columns=['State2', 'State'], index=False)

Solution

  • The easiest solution is probably to read the data as string. Otherwise, the data is read as numerical where leading zeros are dropped. Plus the data is interpreted using decimal numeral system by default, which is not what you'd want anyway.

    pd.read(csv('fieldstatebackup.binetflow', dtype=str)