gpt4 book ai didi

python - 替换/删除 pandas 数据中的某些文本?

转载 作者:行者123 更新时间:2023-12-01 05:09:32 24 4
gpt4 key购买 nike

我遇到了 Pandas 无法正确替换某些文本位的问题...

# Create blank column
csvdata["CTemp"] = ""
# Create a copy of the data in "CDPure"
dcol = csvdata.CDPure
# Fill "CTemp" with the data from "CDPure" and replace and/or remove certain parts
csvdata['CTemp'] = dcol.str.replace(" (AMI)", "").replace(" N/A", "Non")

但是当我打印时,它还没有通过运行 print csvdata[-50:].head(50)

替换任何内容,如下所示
         Pole     KI   DE    Score   STAT  CTemp
4429 NaN NaN NaN 42 NaN Data N/A
4430 NaN NaN NaN 23.43 NaN Data (AMI)
4431 NaN NaN NaN 7.05 NaN Data (AMI)
4432 NaN NaN NaN 9.78 NaN Data
4433 NaN NaN NaN 169.68 NaN Data (AMI)
4434 NaN NaN NaN 26.29 NaN Data N/A
4435 NaN NaN NaN 83.11 NaN Data N/A

注意:CSV 相当大,因此我必须使用 pandas.set_option('display.max_columns', 250) 才能打印上面的内容。

有人知道我如何让它在 pandas 中正确替换这些部件吗?

编辑,我尝试过 .str.replace("", "") 并尝试了 .replace("", "")

示例 CSV:

No,CDPure,Blank
1,Data Test,
2,Test N/A,
3,Data N/A,
4,Test Data,
5,Bla,
5,Stack,
6,Over (AMI),
7,Flow (AMI),
8,Test (AMI),
9,Data,
10,Ryflex (AMI),

示例代码:

# Import pandas
import pandas

# Open csv (I have to keep it all as dtype object otherwise I can't do the rest of my script)
csvdata = pandas.read_csv('test.csv', dtype=object)

# Create blank column
csvdata["CTemp"] = ""
# Create a copy of the data in "CDPure"
dcol = csvdata.CDPure
# Fill "CTemp" with the data from "CDPure" and replace and/or remove certain parts
csvdata['CTemp'] = dcol.str.replace(" (AMI)", "").str.replace(" N/A", " Non")

# Print
print csvdata.head(11)

输出:

    No        CDPure Blank         CTemp
0 1 Data Test NaN Data Test
1 2 Test N/A NaN Test Non
2 3 Data N/A NaN Data Non
3 4 Test Data NaN Test Data
4 5 Bla NaN Bla
5 5 Stack NaN Stack
6 6 Over (AMI) NaN Over (AMI)
7 7 Flow (AMI) NaN Flow (AMI)
8 8 Test (AMI) NaN Test (AMI)
9 9 Data NaN Data
10 10 Ryflex (AMI) NaN Ryflex (AMI)

最佳答案

str.replace 将其参数解释为正则表达式,因此您需要使用 dcol.str.replace(r"\(AMI\)", "") 转义括号.str.replace("N/A", "Non").

这似乎没有充分记录; the docs提到 splitreplace “也采用正则表达式”,但没有明确说明它们始终将其参数解释为正则表达式。

关于python - 替换/删除 pandas 数据中的某些文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24471013/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com