[英]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.