Skip to content
Advertisement

python pandas csv file conversion of integers to binary

I have a csv file like that

Meme1, Meme2, Meme3, Meme4, Meme5, Meme6
Meme1, Meme2, Meme3, Meme99, Meme5, Meme6
Meme5, Meme2, Meme2, Meme4, Meme10, Meme6
Meme99, Meme3, Meme4, Meme4, Meme5, Meme6

I want like that

00000001, 00000010, 00000011, 00000100, 00000101, 00000110
00000001, 00000010, 01100011, 00000100, 00000101, 00000110
00000100, 00000010, 00000010, 00000100, 00001010, 00000110

means every integer should be converted to binary and word meme should be deleted

I am trying but cannot do:(
import pandas as pd
import csv
import numpy as np

dataset = pd.read_csv('datsetcoma.txt')
reader = csv.DictReader(dataset)
print (reader)
 # print back the headers
for row in reader:
    if row.is_integer:
      
       b=np.binary_repr(10, width=8)
       print (b)

Advertisement

Answer

You can also try this:

import pandas as pd
import numpy as np
import io

# example taken from @ifly6  
df = pd.read_csv(io.StringIO('''Meme1, Meme2, Meme3, Meme4, Meme5, Meme6
Meme1, Meme2, Meme3, Meme99, Meme5, Meme6
Meme5, Meme2, Meme2, Meme4, Meme10, Meme6
Meme99, Meme3, Meme4, Meme4, Meme5, Meme6'''), header=None)

df.apply(lambda x: x.apply(lambda y:  bin(int(y.replace('Meme', '')))[2:].zfill(8) ) ) 

#output

          0         1         2         3         4         5
0  00000001  00000010  00000011  00000100  00000101  00000110
1  00000001  00000010  00000011  01100011  00000101  00000110
2  00000101  00000010  00000010  00000100  00001010  00000110
3  01100011  00000011  00000100  00000100  00000101  00000110
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement