python - Pandas Dataframe: How to parse integers into string of 0s and 1s? -


i have following pandas dataframe.

import pandas pd df = pd.read_csv('filename.csv')  print(df)        sample      column_a          0     sample1        6/6     1     sample2        0/4 2     sample3        2/6     3     sample4       12/14    4     sample5       15/21    5     sample6       12/12    ..    .... 

the values in column_a not fractions, , these data must manipulated such can convert each value 0s , 1s (not convert integers binary counterparts).

the "numerator" above gives total number of 1s, while "denominator" gives total number of 0s , 1s together.

so, table should in following format:

      sample      column_a          0     sample1     111111     1     sample2     0000 2     sample3     110000     3     sample4     11111111111100     4     sample5     111111111111111000000  5     sample6     111111111111   ..    .... 

i've never parsed integer output strings of 0s , 1s this. how 1 this? there "pandas method" use lambda expressions? pythonic string parsing or regex?

first, suppose write function:

def to_binary(s):     n_d = s.split('/')     n, d = int(n_d[0]), int(n_d[1])     return '1' * n + '0' * (d - n) 

so that,

>>> to_binary('4/5') '11110' 

now need use pandas.series.apply:

 df.column_a.apply(to_binary) 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -