Understanding yyyymm Updates in Repeated Syntax

Asked 2 years ago, Updated 2 years ago, 23 views

The shape of DataFrame is as follows:
Increase the number of yyyymm (month/month) by 1 month
I would like to extract it (the processing of the contents is just an example),
According to the method below, it will be 201713 after December, so it will not work.

Could you tell me how to update the value of yyyymm in the form of the year?

list=range(0,10)
   i=201710
   for x in list:
   df_new=df [df['nengetsu']<=i]
   print(df_new.shape)    
   i=i+1
 print ("end")

python

2022-09-30 19:44

2 Answers

If you're using pandas, wouldn't it be better to use pandas.date_range()?
Also, I think 'nengetsu' Column should be treated as datetime type instead of int type.

I will write a simple sample below.

import pandas as pd

# Generate appropriate data
df = pd.DataFrame({
    'nengetsu': [201710,]
                 201711,
                 201712,
                 201712,
                 201801,
                 201801,
                 201802,
                 201804,
                 201806,
                 201806]
})

# "nengetsu" column converts to datetime type
df['nengetsu'] = pd.to_datetime(df['nengetsu'], format='%Y%m')

# The code of the question can be modified as follows.
for i in pd.date_range('2017/10', periods=10, freq='M'):
    df_new=df [df['nengetsu']<=i]
    print(df_new.shape)
print ("end")

Also, I don't really understand what I want to get in the end, but if I just want to get a monthly count, using DataFrame.resample() or DataFrame.groupby() is overwhelmingly easier than using loops

print(df.set_index('nengetsu') .resample('MS') .size())


2022-09-30 19:44

You can add an expression under i=i+1 to make December next to January of the following year.

 if(i%100) == 13:
    i+=88 

Also, since the numpy's datetime64 can be in units of months, it can be written simply as follows:

 sr=pd.Series(np.range('2017-10', 10, dtype='datetime64')))
for x in sr:
    df_new=df [df['nengetsu']<=x.year*100+x.month]
    print(df_new.shape)


2022-09-30 19:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.