简体   繁体   中英

Pandas Dataframe Add a value to a new Column based on the previous row limited to the maximum value in that column

It is so hard to learn all the tricks for pandas or working with dataframes.

So I have a df with a certain amount of weeks listed. I wanted to create a metrics table or dataframe that has additional columns added with sequential ascending weeks added only populating the row to the maximum week list in column 'Week'

       Week
 0    201152
 1    201201
 2    201202
 3    201203
 4    201204

The below df is the result i am trying to get to.

       Week  2ndWeek 3rdWeek 4thWeek 5thWeek 
 0    201152  201201  201202  201203  201204
 1    201201  201202  201203  201204
 2    201202  201203  201204  
 3    201203  201204  
 4    201204  

Any ideas?

import pandas as pd
ts = df.Week
for week in range(len(ts) - 1):
    ts = ts.drop(ts.idxmin())
    ts.index = pd.Index(range(len(ts)))
    ts.name = '%s_Week' % week
    df = df.merge(pd.DataFrame(ts), left_index=True,right_index=True, how='outer')

For something like this, you could use shift and a loop. There are some index tricks you can pull but it's unlikely this is a bottleneck, so we might as well be simple.

>>> df = pd.DataFrame({"Week": [201152, 201201, 201202, 201203, 201204]})
>>> df
     Week
0  201152
1  201201
2  201202
3  201203
4  201204

[5 rows x 1 columns]
>>> for n in range(2, len(df)+1):
...     df["{}_Week".format(n)] = df["Week"].shift(-(n-1))
...     
>>> df
     Week  2_Week  3_Week  4_Week  5_Week
0  201152  201201  201202  201203  201204
1  201201  201202  201203  201204     NaN
2  201202  201203  201204     NaN     NaN
3  201203  201204     NaN     NaN     NaN
4  201204     NaN     NaN     NaN     NaN

[5 rows x 5 columns]

If you really want '' instead of NaN , you could add .fillna('') , but even though they're not as nice to look at, arithmetic will work much better with NaN s, as they're interpreted as missing values by many of the routines.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM