2016-08-22 132 views
0

在CSV改变一个确切值我有一个CSV文件,该文件类似于这样:使用熊猫

id | item_name1    | item_name2 
-------------------------------------------------- 
1 | unclassified   | text 
2 | SantaCruz unclassified | text 
3 | text     | text 
4 | texttext    | text 
5 | unclassified   | text 
6 | unclassified text  | unclassified 
7 | text     | unclassified text 
8 | text     | text 
9 | text     | text 
.. | ..      | .. 
1000 | unclassified text | text 

我试图消除所有的细胞,只有说“机密”;即诸如“SantaCruz未分类”的单元应该保持不变。

我发现了很多使用替换函数删除特定单词的例子,但是还没有找到任何仅用于替换完全匹配的单元格的示例。

我正在使用熊猫,并且能够打开csv,打印等,但在解决此特定问题时遇到了问题。任何帮助将不胜感激!

由于

回答

1

pandas.Series.str.replace可以采取regex作为参数。假设您要替换的字符串“未分类”中没有前后空格,则该正则表达式应为^unclassified$

df['item_name1'].str.replace('^unclassified$', 'replaced_string') 

0   replaced_string 
1 SantaCruz unclassified 
2      text 
3     texttext 
4   replaced_string 
5   unclassified text 
6      text 
7      text 
8      text 
9   unclassified text 
Name: item_name1, dtype: object