简体   繁体   中英

How to flip column if certain condition is meet pandas

I have question regarding pandas. Lets assume that I have following table

| condition |  value |
|-----------|--------|
| Yes       |  0     |
|-----------|--------|
| Yes       |  1     |
|-----------|--------|
| Yes       |  2     |
|-----------|--------|
| Yes       |  3     |
|-----------|--------|
| Yes       |  4     |
|-----------|--------|
| No        |  0     |
|-----------|--------|
| Yes       |  4     |
|-----------|--------|
| Yes       |  5     |
|-----------|--------|
| Yes       |  6     |
|-----------|--------|
| No        |  0     |
|-----------|--------|

I want to get something like following

| condition |  value |
|-----------|--------|
| Yes       |  4     |
|-----------|--------|
| Yes       |  3     |
|-----------|--------|
| Yes       |  2     |
|-----------|--------|
| Yes       |  1     |
|-----------|--------|
| Yes       |  0     |
|-----------|--------|
| No        |        |
|-----------|--------|
| Yes       |  2     |
|-----------|--------|
| Yes       |  1     |
|-----------|--------|
| Yes       |  0     |
|-----------|--------|
| No        |        |
|-----------|--------|

For this i though I can do loop to get result like:

continue_condition = False
for index, row in df.iterrows():
    if row['condition'] == 'Yes': 
       if not continue_condition:
           continue_condition = True
           min_value = row['value']

    else:
       continue_condition = False
       max_value = row['value']
       start_point = max_value - min_value
       point_value = 0
       while point_value <= start_point:
           df.loc[index-point_value, "new_value"] = point_value
           point_value += 1
        min_value = None
        max_value = None
        point_value = None

However, this is slow code, just wondering if how can i speed up code?

This is one solution :

 condition = [('Yes',0),('Yes',1), ('Yes',2), ('Yes',3),('Yes',4),('No',0),('Yes',4),('Yes',5),('Yes',6),('No',0)]

 df = pd.DataFrame(condition,columns=['condition','value'])

numpy flip 'flips' the arrays

(pd
 .DataFrame(np.flip(df.to_numpy(),1), 
            columns=['value','condition'])

 #create helper columns 

 .assign(check = lambda x: x.value.diff(-1),
         temp = lambda x: np.where(x.check.abs() > 1, 
                                   x.check.abs(),np.nan)
        )
 .assign(temp = lambda x: x.temp.fillna(method = 'bfill'),
         value = lambda x: np.where(x.condition=="Yes",
                                    x.value.sub(x.temp).abs(),"")
         )
 .filter(['condition','value'])
  )


  condition value
0   Yes 4
1   Yes 3
2   Yes 2
3   Yes 1
4   Yes 0
5   No  
6   Yes 2
7   Yes 1
8   Yes 0
9   No  

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