从数据框中更改列名
Changing column names from dataframe
我有一个数据框,其中包含标准普尔 500 指数中列出的所有公司及其每日数据。但是,在每个数据框列名中都有一个不需要的 "/n"
附加到它,我想将其删除。
我收到以下错误:
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)
这是导致错误的函数:
def remove_extra_characters(df):
df.columns = df.columns.str.replace('*.\n.*', '')
给你!新线不见了! :)
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)
我有一个数据框,其中包含标准普尔 500 指数中列出的所有公司及其每日数据。但是,在每个数据框列名中都有一个不需要的 "/n"
附加到它,我想将其删除。
我收到以下错误:
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)
这是导致错误的函数:
def remove_extra_characters(df):
df.columns = df.columns.str.replace('*.\n.*', '')
给你!新线不见了! :)
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)