【问题标题】:Figuring out if all data in a pandas DataFrame row is the same except for a particular column确定除了特定列之外,pandas DataFrame 行中的所有数据是否相同
【发布时间】:2022-01-18 06:28:06
【问题描述】:

我能够在 SO 的帮助下完成这项工作:

# Input string changes but the one I need is always passed in
input_string = "random string"

df.loc[input_string, df.drop(input_string).eq(111).all()] = 111

上面的代码本质上是一列,并检查该列中 DataFrame 的所有单元格值是否除了input_string 指定的单元格值都是 111,如果是,它也将其设置为 111。

我该如何为一行执行此操作?

【问题讨论】:

    标签: python pandas dataframe python-3.7 pandas-loc


    【解决方案1】:

    我认为你的逻辑是倒退的。您当前正在做的实际上是删除一行并检查其他行是否符合您的条件。见下文:

    #do column checking
    df = pd.DataFrame({'a':[111]*10,'b':[111]*10})
    df_col = df.copy() 
    input_col = 'a'
    df_col[input_col] = [222]*len(df.index) #distort input - should be removed in next step
    df_col.loc[df_col.drop(input_col,axis=1).eq(111).all(1), input_col] = 111
    
    #do row checking
    df_row = df.copy()
    input_row = 2
    df_row.iloc[input_row] = [222]*len(df.columns) #distort input - should be removed in next step
    df_row.loc[input_row,df_row.drop(input_row).eq(111).all()] = 111
    

    【讨论】:

      猜你喜欢
      • 2022-11-20
      • 2022-11-17
      • 2016-09-27
      • 2018-11-23
      • 2015-09-08
      • 2018-04-28
      • 1970-01-01
      • 2019-08-20
      • 1970-01-01
      相关资源
      最近更新 更多