代码之家  ›  专栏  ›  技术社区  ›  MarkS

将列中的值更改为np。基于行索引的nan

  •  1
  • MarkS  · 技术社区  · 4 年前

    我想有选择地将列值更改为np。楠。

    我有一列有很多零(0)值。

    我得到的是一个子集的行索引。

    我将指数放入一个变量(s0)。

    然后我用它将列值设置为np。nan仅用于索引为s0的行。

    它会运行,但会将每一行(即整个列)都更改为np。楠。

    这是我的代码:

    print((df3['amount_tsh'] == 0).sum())  # 41639  <-- there are this many zeros to start
    # print(df3['amount_tsh'].value_counts()[0])
    s0 = df3['amount_tsh'][df3['amount_tsh'].eq(0)].sample(37322).index  #  grab 37322 row indexes
    print(len(s0))  # 37322
    df3['amount_tsh'] = df3.loc[df3.index.isin(s0), 'amount_tsh'] = np.nan  #  change the value in the column to np.nan if it's index is in s0
    print(df3['amount_tsh'].isnull().sum())
    
    2 回复  |  直到 4 年前
        1
  •  0
  •   wwnde    4 年前

    让我们试试

    s0 = df3.loc[df3['amount_tsh'].eq(0), ['amount_tsh']].sample(37322)
    df3.loc[df3.index.isin(s0.index), 'amount_tsh'] = np.nan
    

    为了快速解决这个问题,我使用了笔记本中的数据,它对我很有效

    import pandas as pd 
    import numpy as np
    
    data = pd.DataFrame({'Symbol': {0: 'ABNB', 1: 'DKNG', 2: 'EXPE', 3: 'MPNGF', 4: 'RDFN', 5: 'ROKU', 6: 'VIACA', 7: 'Z'},
    'Number of Buys': {0: np.nan, 1: 2.0, 2: np.nan, 3: 1.0, 4: 2.0, 5: 1.0, 6: 1.0, 7: np.nan}, 
    'Number of Sell      s': {0: 1.0, 1: np.nan, 2: 1.0, 3: np.nan, 4: np.nan, 5: np.nan, 6: np.nan, 7: 1.0}, 
    'Gains/Losses': {0: 2106.0, 1: -1479.2, 2: 1863.18, 3: -1980.0, 4: -1687.7, 5: -1520.52, 6: -1282.4, 7: 1624.59}, 'Percentage change': {0: 0.0, 1: 2.0, 2: 0.0, 3: 0.0, 4: 1.5, 5: 0.0, 6: 0.0, 7: 0.0}})
    
    rows = ['ABNB','DKNG','EXPE']
    data
    
    
      Symbol  Number of Buys  Number of Sell      s  Gains/Losses  \
    0   ABNB             NaN                    1.0       2106.00   
    1   DKNG             2.0                    NaN      -1479.20   
    2   EXPE             NaN                    1.0       1863.18   
    3  MPNGF             1.0                    NaN      -1980.00   
    4   RDFN             2.0                    NaN      -1687.70   
    5   ROKU             1.0                    NaN      -1520.52   
    6  VIACA             1.0                    NaN      -1282.40   
    7      Z             NaN                    1.0       1624.59   
    
       Percentage change  
    0                0.0  
    1                2.0  
    2                0.0  
    3                0.0  
    4                1.5  
    5                0.0  
    6                0.0  
    7                0.0 
    

    按你的方法

    (data['Number of Buys']==1.0).sum()
    s0= data.loc[(data['Number of Buys']==1.0),['Number of Buys']].sample(2)
    data.loc[data.index.isin(s0.index),'Number of Buys'] =np.nan
    
    Symbol  Number of Buys  Number of Sell      s  Gains/Losses  \
    0   ABNB             NaN                    1.0       2106.00   
    1   DKNG             2.0                    NaN      -1479.20   
    2   EXPE             NaN                    1.0       1863.18   
    3  MPNGF             1.0                    NaN      -1980.00   
    4   RDFN             2.0                    NaN      -1687.70   
    5   ROKU             NaN                    NaN      -1520.52   
    6  VIACA             NaN                    NaN      -1282.40   
    7      Z             NaN                    1.0       1624.59   
    
       Percentage change  
    0                0.0  
    1                2.0  
    2                0.0  
    3                0.0  
    4                1.5  
    5                0.0  
    6                0.0  
    7                0.0  
    
        2
  •  0
  •   MarkS    4 年前

    隐马尔可夫模型。。。

    我删除了重新分配的任务,它成功了??

    s0=df3[“金额”][df3[“金额”]。等式(0)]。样本(37322)。指数 df3。loc[df3.索引isin(s0),“金额”]=np。楠

    第二行是: df3['amount_tsh']=df3。loc[df3.索引isin(s0),“金额”]=np。楠

    推荐文章