简体   繁体   English

使用 for 循环替换 pandas 列的每一行中的单元格值

[英]Replace cell values in each row of pandas column using for loop

Please, help me understand my error.请帮助我理解我的错误。 I'm trying to change one column in my .csv file.我正在尝试更改.csv文件中的一列。 I have .csv file as following:我有.csv文件如下:

sku,name,code  
k1,aaa,886  
k2,bbb,898  
k3,ccc,342  
k4,ddd,503  
k5,eee,401  

I want to replace "k" symbol with the "_" symbol in the "sku" column.我想用“sku”列中的“_”符号替换“k”符号。
I wrote the code:我写的代码:

import sys  
import pandas as pd  
import numpy as np  
import datetime  

df = pd.read_csv('cat0.csv')  

for r in df['sku']:  
    r1 = r.replace('k', '_')  
    df['sku'] = r1  

print (df) 

But the code inserts the last value in every row of the "sku" column.但是代码在“sku”列的每一行中插入最后一个值。 So I get:所以我得到:

  sku name  code
0  _5  aaa   886
1  _5  bbb   898
2  _5  ccc   342
3  _5  ddd   503
4  _5  eee   401

I want to get as following:我想得到如下:

  sku name  code
0  _1  aaa   886
1  _2  bbb   898
2  _3  ccc   342
3  _4  ddd   503
4  _5  eee   401

You can use str.replace on the whole column:您可以在整列上使用str.replace

from io import StringIO
import pandas as pd

data = """sku,name,code  
k1,aaa,886  
k2,bbb,898  
k3,ccc,342  
k4,ddd,503  
k5,eee,401"""

file = StringIO(data)

df = pd.read_csv(file)
df['sku'] = df['sku'].str.replace('k', '_')

print(df)

This yields这产生

  sku name  code  
0  _1  aaa     886
1  _2  bbb     898
2  _3  ccc     342
3  _4  ddd     503
4  _5  eee     401

As @Jan mentioned, doing it by using df['sku'] = df['sku'].str.replace('k', '_') is the best/quickest way to do this.正如@Jan 提到的,通过使用df['sku'] = df['sku'].str.replace('k', '_')来做到这一点是最好/最快的方法。

However, to understand why you are getting the results you are and to present a way as close to how you were doing it as possible, you'd do:但是,要了解您为什么会得到这样的结果并尽可能地展示与您的工作方式相近的方式,您可以这样做:

import pandas as pd

df = pd.DataFrame(
    {
        'sku':["k1", "k2", "k3", "k4", "k5"], 
        'name': ["aaa", "bbb", "ccc", "ddd", "eee"], 
        'code':[886, 898,342,503,401]
    }, columns =["sku", "name", "code"]
)

for i, r in enumerate(df['sku']):  
    r1 = r.replace('k', '_')
    df.at[i, 'sku'] = r1  

Which gives:这使:

  sku name  code
0  _1  aaa   886
1  _2  bbb   898
2  _3  ccc   342
3  _4  ddd   503
4  _5  eee   401

In your code...在您的代码中...

for r in df['sku']:  
    r1 = r.replace('k', '_')  

...the issue is here: ...问题在这里:

    df['sku'] = r1  

You are broadcasting your results to the entire column rather than just the row you are working on.您将结果广播到整个列,而不仅仅是您正在处理的行。

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

相关问题 使用 for 循环替换 Pandas 中每一行和每一列的单元格值 - Replace cell values for each row and each column in Pandas using for loop 使用binarizer和for循环替换pandas列每一行中的单元格值 - Replace cell values in each row of pandas column using for binarizer and for loop 循环 pandas 列并替换值 - Loop pandas column and replace values 使用 Loop 替换数据框中列中的每一行,并使用它出现的实例 - Using Loop to replace each row in a column in a dataframe wtih the instance that it appears 仅当整行(每列)具有 NaN 值时,将 pandas 中的行替换为下一行 - Replace row in pandas with next row only when the entire row (each column) has NaN values 如何使用 pandas 循环遍历一行中的每一列 - how to loop through each column in a row using pandas 为 pandas dataframe 的每一行替换列中的字符串 - Replace a string in a column for each row of a pandas dataframe 熊猫:如果满足条件,如何使用for循环替换列中的值 - Pandas: How to replace values in a column using for loop if conditions are met Python Pandas DataFrame:根据条件替换每一列的每一行中的值 - Python Pandas DataFrame: Replace values in each row for each column based on conditions 熊猫替换每列中的某些值 - Pandas Replace certain values in each column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM