从列标题中删除特殊字符 \r 回车符。

huangapple go评论58阅读模式
英文:

Remove special character \r carriage return from column headers

问题

如何删除csv文件标题中的特殊字符?

Col1\r -> 列名

Col1\r 中,我们基本上会将其视为 Col1,在Excel中打开时实际上是看不到的。

我已将数据移动到一个名为 df 的数据框中。

# 从列标题中删除特殊字符
dfcolumns = df.columns
英文:

How do we remove special characters in the header of a csv file?

Col1\r -> Column name

In Col1\r we would basically see it as Col1, which is actually not visible when opened in Excel.

I have moved the data to a dataframe df.

# Remove special characters from column headers  
dfcolumns = df.columns

答案1

得分: 1

你可以尝试类似这样的操作:

df.columns = df.columns.str.replace('\r', '')

更多信息请参考此处

英文:

You can try something like this:

df.columns = df.columns.str.replace('\r', '')

More information here.

答案2

得分: 0

Depends on where are you taking the column name from. If it's a json you need to open it with the proper encoding. If you have this string handled by another variable in plain text, you need to manipulate it and clean the bad values. with

string_variable.replace('\r', '')
英文:

Depends on where are you taking the column name from. If it's a json you need to open it with the propper encoding. If you have this string handled by another varaiable in plain text, you need to manipulate it and clean the bad values. with

string_variable.replace('\r', '')

huangapple
  • 本文由 发表于 2023年5月25日 22:40:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76333527.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定