Python 为熊猫中的一组列设置新值

Python 为熊猫中的一组列设置新值,python,pandas,data-science,Python,Pandas,Data Science,我有一个案例应该可以解决,但我的解决方案太慢(慢了几个小时): 我有一个数据集,根据一个条件,我需要更改一些列的值 我写了代码: for i, row in df.iterrows(): if row.specific_column_value == 1: continue col_names = ['A1', 'A2', ..., 'An'] new_values = [1, 2, 3, 4, .., n] for j, col in enu

我有一个案例应该可以解决,但我的解决方案太慢(慢了几个小时):

我有一个数据集,根据一个条件,我需要更改一些列的值

我写了代码:

for i, row in df.iterrows():
    if row.specific_column_value == 1:
        continue

    col_names = ['A1', 'A2', ..., 'An']
    new_values = [1, 2, 3, 4, .., n]

    for j, col in enumerate(col_names):
        df.loc[i, col] = new_values[j]
这是非常缓慢的


如何加快速度?

您可以使用
分配新值,然后根据您的条件分配
.loc

df.loc[ df.order_received == 1, col_names ] = new_values

更新

for i, row in df.iterrows():
    if row.specific_column_value == 1:
        col_names = ['A1', 'A2', ..., 'An']
        new_values = [1, 2, 3, 4, .., n]
        df.loc[i, col_names ] = new_values 

不,df_-specific_列_值=1对于许多行都是真的,因此我必须loop@tstseby那是多行作业,你能给我们看一些样本数据吗,或者你想让我做一个吗?但最好让我们看到预期的结果output@tstsebyIIUC,这里的逻辑是,如果该行等于1,您将为该行中的列分配newq值,这是正确的吗?我得到了ValueError:Uncompatible indexer with series->我只需要用更快的东西替换内部for循环->我用df.specific_column_值替换为index尝试了你的解决方案,我得到了上面的错误谢谢,它可以工作(我肯定我必须早些时候尝试过),但速度仍然很慢,我的数据将花费数小时和数小时的时间,有什么方法可以让它更快?pandas对此是否有更快的操作。通过阅读您的代码,我想您应该尝试删除外部for循环一次。。它只是在行上迭代(不必要)…@tstseby,这有助于减少运行时间吗?
If you have limited number of columns(n), you may be able to reduced the search 
operation to O(n) instead of O(m x n) complexity that you have in current approach

inx_collection = set()
value_looking_for = 1
col_values = [1, 2, 3, 4, .., n]
for col in df.columns:
    inx = df.index[df[col] == value_looking_for]
    inx_collection.update(inx) # This set collects all indices containing the value
df.loc[inx_collection,:] = col_value