Big Data 5 min read

Generating Sequential Dates and Reordering Columns in Pandas for FIFO Data

This article walks through a Pandas data‑manipulation problem where a user needs to generate sequential dates, convert column types, reorder columns, and produce a FIFO‑style dataset, providing the original code, a step‑by‑step solution with code snippets and illustrative images.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Generating Sequential Dates and Reordering Columns in Pandas for FIFO Data

1. Introduction

Hello, I'm PiPi. A user in a Python community asked for a Pandas solution to generate date data for a FIFO‑style table. The original data and the desired result are shown below.

The user also wondered whether the solution works when there are multiple products.

Here is the original code:

data = [[
    ['铅笔','入','60','3.5','京东'],
    ['钢笔','入','35','5.5','京东'],
    ['钢笔','出','21','10.9','A公司'],
    ['铅笔','出','31','5','B公司'],
    ['铅笔','入','120','3.2','苏宁'],
    ['钢笔','入','5','6','国美'],
    ['铅笔','出','88','4','B公司'],
    ['钢笔','出','10','7','C公司'],
    ['牙膏','入','150','6.5','苏宁'],
    ['牙膏','出','110','7','A公司'],
    ['铅笔','出','20','7','B公司']
]

df = pd.DataFrame(data=data, columns=['品名','入/出','数量','单价/元','供应商/购买方'])

df[['数量','单价/元']] = df[['数量','单价/元']].apply(lambda x: x.astype(float))  # data type conversion

df['日期'] = pd.date_range('2023-11-15', periods=len(df))  # add date column

df = df.take([5,0,1,2,3,4], axis=1)  # reorder columns

2. Implementation

A contributor suggested a simple approach: cumulative subtraction. The following image illustrates the idea.

The contributor later simplified the code, as shown in the next image.

The problem was solved successfully.

3. Conclusion

This article presented a Pandas data‑filtering issue, explained the problem, and provided a concrete implementation to help the community solve similar data‑analysis tasks.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

data analysisDate GenerationFIFO
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.