Search code examples
pythondataframematrixbit-manipulationminimum

How do you print the complete bit without the prefixed zeroes being ignored?


I have a DF which has values like:

       col1      col2        col3
row1   0010010   0011010     0010011
row2   0000011   0100011     1111111
row3   0101010   1101101     1100101

I am trying to fetch the minimum value of each column but, when I do a:

df.min (axis = 0,skipna = True)

I get the minimum values of each column however the initial zeroes are ignored and I get the rest of the values. For example, if the minimum value is 0010010 from row1 col1, I get 10010.0 in float.

How do I print all the bits without the zeroes prefixed being ignored?


Solution

  • I can suggest you 2 methods to achieve this.

    1. If you are sure that the numbers will always be of 7 digits, convert all the values into string inside the dataframe.

    You can convert it during reading csv into dataframe:

    df = read_csv('myfile.csv', converters={'columnName': lambda x: str(x)})
    

    You can convert it after you have data in your df.

    df['columnName'] = df['columnName'].astype('str')
    

    2. If you just want to print the values with proper formatting with leading zeroes, you can specify formatter while printing like below :

    val = 10010.0
    print("%07.0f" % val)
    
    --> output = 0010010