简体   繁体   English

在循环 dataframe 时抓取 pandas 列的更新行

[英]Grab Updated rows of pandas column while looping through dataframe

I am trying the following:我正在尝试以下操作:

import pandas as pd
df = pd.DataFrame({'Col1': {0: 'A', 1: 'A', 2: 'B', 3: 'B', 4: 'B'},
 'Col2': {0: 'a', 1: 'a', 2: 'b', 3: 'b', 4: 'c'},
 'Col3': {0: 42, 1: 28, 2: 56, 3: 62, 4: 48}})

ii = 1
for idx, row in df.iterrows():
    print(row)
    df.at[:, 'Col2'] = 'asd{}'.format(ii)
    ii += 1

But the print statement above doesn't reflect the change df.at[:, 'Col2'] = 'asd'.format(ii) .但是上面的 print 语句没有反映df.at[:, 'Col2'] = 'asd'.format(ii)的变化。 I need the print statements to reflect the change df.at[:, 'Col2'] = 'asd'.format(ii)我需要打印语句来反映更改df.at[:, 'Col2'] = 'asd'.format(ii)

Edit: Since I am updating all rows of df , I was expecting the idx and row to grab new values from dataframe.编辑:因为我正在更新df的所有行,所以我期望idx and row从 dataframe 获取新值。

If this is not the right way to grab updated values from df through idx and row , then what is the correct approach.如果这不是通过idx and rowdf获取更新值的正确方法,那么正确的方法是什么。 I need idx and row to reflect new values.我需要idx and row来反映新值。

Expected output:预计 output:

Col1     A
Col2     a
Col3    42
Name: 0, dtype: object
Col1     A
Col2     asd1
Col3    28
Name: 1, dtype: object
Col1     B
Col2     asd2
Col3    56

.....

From iterrows documentation :来自iterrows文档

You should never modify something you are iterating over.你永远不应该修改你正在迭代的东西。 This is not guaranteed to work in all cases.这不能保证在所有情况下都有效。 Depending on the data types, the iterator returns a copy and not a view, and writing to it will have no effect.根据数据类型,迭代器返回副本而不是视图,写入它不会有任何效果。

As per your request for an alternative solution, here is one using DataFrame.apply :根据您对替代解决方案的要求,这是一个使用DataFrame.apply的解决方案:

df['Col2'] = df.apply(lambda row: 'asd{}'.format(row.name), axis=1)

Other examples (also using Series.apply ) that may be useful for your eventual goal: (not clear what it is yet)可能对您的最终目标有用的其他示例(也使用Series.apply ):(尚不清楚它是什么)

df['Col2'] = df['Col2'].apply(lambda x: 'asd{}'.format(x))
df['Col2'] = df.apply(lambda row: 'asd{}'.format(row['Col3']), axis=1)

Here is something you can try,这是你可以尝试的东西,

import pandas as pd

df = pd.DataFrame({'Col1': {0: 'A', 1: 'A', 2: 'B', 3: 'B', 4: 'B'},
                   'Col2': {0: 'a', 1: 'a', 2: 'b', 3: 'b', 4: 'c'},
                   'Col3': {0: 42, 1: 28, 2: 56, 3: 62, 4: 48}})

print(
    df.assign(idx=df.index)[['idx', 'Col2']]
        .apply(lambda x: x['Col2'] if x['idx'] == 0 else f"asd{x['idx']}", axis=1)
)

0       a
1    asd1
2    asd2
3    asd3
4    asd4
dtype: object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM