如何删除 DataFrame 中字符串中的特殊字符,如“,”?
How do I remove special character like "," within a string in a DataFrame?
我有一个如下所示的数据框类型:pandas.core.frame.DataFrame
newdf = pd.DataFrame([
[0, "Hello, How are you?", 1],
["I wish, we could get it right", 0, 0],
[0, "Hey there, hope you are doing well", 0]],
columns=['Mon','Tue','Wed'])
newdf
|Mon | Tue | Wed |
|:------------------------------|:----------------------------------:|-----:|
| 0 | Hello, How are you? | 1 |
|I wish, we could get it right | 0 | 0 |
| 0 |Hey there, hope you are doing well | 0 |
我想保存为CSV格式上传到MySQL,但是句子中有带“,”的字符串
我想用“”或“-”替换所有“,”
我试过了
newdf1 = newdf.replace(",","-")
数据框没有变化
请帮助替换 DataFrame 中字符串中的字符。
我之前使用 .replace()
更改单元格的整个元素并且它有效,但现在我想读取字符串内部并执行操作。
您必须首先为子字符串匹配启用正则表达式:
>>> newdf.replace(",", "-", regex=True)
Mon Tue Wed
0 0 Hello- How are you? 1
1 I wish- we could get it right 0 0
2 0 Hey there- hope you are doing well 0
>>>
我有一个如下所示的数据框类型:pandas.core.frame.DataFrame
newdf = pd.DataFrame([
[0, "Hello, How are you?", 1],
["I wish, we could get it right", 0, 0],
[0, "Hey there, hope you are doing well", 0]],
columns=['Mon','Tue','Wed'])
newdf
|Mon | Tue | Wed |
|:------------------------------|:----------------------------------:|-----:|
| 0 | Hello, How are you? | 1 |
|I wish, we could get it right | 0 | 0 |
| 0 |Hey there, hope you are doing well | 0 |
我想保存为CSV格式上传到MySQL,但是句子中有带“,”的字符串
我想用“”或“-”替换所有“,”
我试过了
newdf1 = newdf.replace(",","-")
数据框没有变化
请帮助替换 DataFrame 中字符串中的字符。
我之前使用 .replace()
更改单元格的整个元素并且它有效,但现在我想读取字符串内部并执行操作。
您必须首先为子字符串匹配启用正则表达式:
>>> newdf.replace(",", "-", regex=True)
Mon Tue Wed
0 0 Hello- How are you? 1
1 I wish- we could get it right 0 0
2 0 Hey there- hope you are doing well 0
>>>