具有固定数量数字的列

column with a fixed number of numbers

我正在尝试使用 pandas 使 excel 工作自动化。

我有一个包含列 'reference' 的给定数据库,该列应该有 2 个数字(例如:25、53、45...)。

我需要让列中的所有行都包含两个数字,但像 1、2、3、4 到 9 这样的引用只有 1 个数字。

我可以添加一个函数来将此列的整行转换为两个数字引用吗?

我已经尝试过 if 语句,但没有给出任何结果。

对不起我的英语,我希望我能理解 :)

导入 pandas 作为 pd

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

参考##column 2##column 3

1黄7474 10棕8220 43白29374 45黑993 2棕9220 5棕2929 39黑3683

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

.....

我的输出必须是这样的:

参考##column 2##column 3

01黄7474 10棕8220 43白29374 45黑993 02棕9220 05棕2929 39黑3683

您是否可以 post 一个关于您所拥有的和您正在寻找的数据样本?

要阅读您的 excel 文档,请使用:

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

一般来说,如果您在 pandas 中有一个 'reference' 列,我们会将其称为 'index' 列。

您可以通过以下方式设置索引:

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

如果您尝试在数字(小于 9)前添加零,则需要将其转换为文本字符串。然后你可以使用 zfill 来 'fill' in the 0 for the numbers less than 2 digits.

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

祝你好运!