[英]Changing column names from dataframe
I have a dataframe with all the companies listed in the S&P500 with their daily data.我有一个 dataframe 与所有在标准普尔 500 指数中列出的公司及其每日数据。 However in each dataframe column name there is an undesired
"/n"
appended to it and I want to remove it.但是,在每个 dataframe 列名中都附加了一个不需要的
"/n"
,我想将其删除。
I get the following error:我收到以下错误:
df.columns = df.columns.str.replace('*.\n.*', '')
File "C:\Python37\lib\site-packages\pandas\core\strings.py", line 1843, in wrapper
return func(self, *args, **kwargs)
File "C:\Python37\lib\site-packages\pandas\core\strings.py", line 2716, in replace
self._parent, pat, repl, n=n, case=case, flags=flags, regex=regex
File "C:\Python37\lib\site-packages\pandas\core\strings.py", line 619, in str_replace
compiled = re.compile(pat, flags=flags)
File "C:\Python37\lib\re.py", line 234, in compile
return _compile(pattern, flags)
File "C:\Python37\lib\re.py", line 286, in _compile
p = sre_compile.compile(pattern, flags)
File "C:\Python37\lib\sre_compile.py", line 764, in compile
p = sre_parse.parse(p, flags)
File "C:\Python37\lib\sre_parse.py", line 930, in parse
p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, 0)
File "C:\Python37\lib\sre_parse.py", line 426, in _parse_sub
not nested and not items))
File "C:\Python37\lib\sre_parse.py", line 651, in _parse
source.tell() - here + len(this))
re.error: nothing to repeat at position 0 (line 1, column 1)
This is the function causing the error:这是导致错误的 function:
def remove_extra_characters(df):
df.columns = df.columns.str.replace('*.\n.*', '')
Here you go: New-lines be gone! go 在这里:新行不见了! :)
:)
import pandas as pd
data = [['tom', 10], ['nick', 15], ['juli', 14]]
df = pd.DataFrame(data, columns=['Name\n\n\n', 'Age\n\n'])
df.rename(columns={old:old.strip() for old in df.columns}, inplace=True)
print(df)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.