有条件地创建新的dataframe列以显示现有列的内容

时间:2018-05-17 04:53:40

标签: python pandas

我有一个pandas数据框,它在下面读到

SKU  ClosingBalance
S1       10
S2       np.nan
S3       0
S4       20

我想创建一个新列,它只显示具有正收盘余额的SKU,其余部分应标记为np.nan,如下所示

SKU      ClosingBalance    SKU_CB
S1       10                S1
S2       np.nan            np.nan
S3       0                 np.nan
S4       20                S4

我试过

conditions = [(df['ClosingBalance'] > 1)]
df['SKU_CB'] = np.select(conditions, df['SKU'], default=np.nan)

不工作。需要你的帮助。

2 个答案:

答案 0 :(得分:4)

所以你的数据 实际上包含带NaN的数字数据,所以这就足够了 -

pd.Series.mask / where

df['SKU_CB'] = df.SKU.where(df.ClosingBalance.gt(0))

或者,

df['SKU_CB'] = df.SKU.mask(~df.ClosingBalance.gt(0))

df
  SKU  ClosingBalance SKU_CB
0  S1            10.0     S1
1  S2             NaN    NaN
2  S3             0.0    NaN
3  S4            20.0     S4

如果偶然的话,您的数据的NaN-ish值不完全是NaN,那么这里是另一种选择。使用pd.to_numeric确定哪些值无效,并使用它来屏蔽SKU

pd.to_numeric pd.Series.mask / where

df['SKU_CB'] = df.SKU.mask(
    ~pd.to_numeric(df.ClosingBalance, errors='coerce').gt(0)
)

或者,

df['SKU_CB'] = df.SKU.where(
   pd.to_numeric(df.ClosingBalance, errors='coerce').gt(0)
)

df
  SKU ClosingBalance SKU_CB
0  S1             10     S1
1  S2         np.nan    NaN
2  S3              0    NaN
3  S4             20     S4

答案 1 :(得分:3)

您可以使用np.where

执行此操作
.
.
@Component({
    .
    .
})
export class Component1 {
    constructor(private eventProxyService: EventProxyService){}
    onClick(){
        console.log(“onClick clicked”)
        this.eventProxyService.change(“1”)
    }
}

import { Injectable } from '@angular/core'
import { Observable } from 'rxjs/Observable'
import { Subject } from 'rxjs/Subject'
@Injectable()
export class EventProxyService {
    private dataSource = new Subject<any>()
    data$ = this.dataSource.asObservable()
    change(param: any) {
        console.log("in service", param)
        this.dataSource.next(param)
    }
}


@Component({
.
.
})
export class Component2 implements OnInit {
    constructor(private eventProxyService: EventProxyService){}
    ngOnInit() {

        this.eventProxyService.data$.subscribe((param: any) => {
            console.log("in jobs", param)
        })
    }
}
import { NgModule } from '@angular/core';
Import { EventProxyService } from ‘event-proxy.service’
@NgModule({
    .
    .
    providers: [
        EventProxyService 
    ],
    .
    .
})
export class AppModule { }

或者,在您使用np.select时,您似乎遇到了第二个参数的问题:

  

choicelist:ndarrays列表从中获取输出元素的数组列表。它必须与condlist的长度相同。

所以它应该是

df['SKU_CB'] = np.where(df['ClosingBalance'] > 1, df['SKU'], np.nan)