Search code examples
pythonexcelpandascharacterminimum

column with a fixed number of numbers


I'm trying to automate an excel work using pandas.

I have a given database with a column 'reference', this column should have 2 numbers (example: 25, 53, 45...).

I need to have all the rows in the column with two numbers but references like 1, 2, 3, 4 until 9 are only 1 number.

Is there a function I can add in order to transform the whole rows of this column to two numbers reference?

I have already tried the if-statement but it didn't give any result.

sorry for my English, I hope I made myself understood :)

import pandas as pd

df = pd.read_excel('file.xlsx')

reference# #column 2# #column 3

1 Yellow 7474 10 brown 8220 43 white 29374 45 black 993 2 brown 9220 5 brown 2929 39 black 3683

df.set_index('reference', inplace=True)

.....

My output has to be like this :

reference# #column 2# #column 3

01 Yellow 7474 10 brown 8220 43 white 29374 45 black 993 02 brown 9220 05 brown 2929 39 black 3683


Solution

  • Is it possible for you to post a data sample of what you have and what you are looking for?

    To read in your excel document use:

    df = pd.read_excel('excel_doc.xlsx')
    

    In general, if you have a 'reference' column in pandas we would call this the 'index' column.

    You can set the index by going:

    df.set_index('reference_column',inplace=True)
    

    If you are trying to add a zero in front of the numbers (less than 9) you need to turn it into a text string. You can then use zfill to 'fill' in the 0 for the numbers less than 2 digits.

    df['column_of_numbers'].str.zfill(2)
    

    Good luck!