【问题标题】:ParserWarning and Future Warning Python ErrorsParserWarning 和未来警告 Python 错误
【发布时间】:2020-02-15 14:06:35
【问题描述】:

我正在尝试运行 python 代码来重新排列我的一些文件:

import glob
import pandas as pd
import numpy as np
files = glob.glob('*.txt')
result = pd.DataFrame()
for file in files:
    df = pd.read_csv(file,delimiter='  ')
    current_col = df.columns[0]
    df.reset_index(inplace=True)
    df.set_index(current_col,inplace=True)
    df.index.name = 'index'
    df.rename(columns={'index':current_col}, inplace=True)
    result = pd.concat([result,df],axis=1)

但是我得到这个错误:

file4.py:9: ParserWarning: 回退到 'python' 引擎,因为 'c' 引擎不支持正则表达式分隔符(分隔符 > 与 '\s+' 不同的 1 个字符被解释为正则表达式);你可以 通过指定 engine='python' 来避免这个警告。 df = pd.read_csv(file,delimiter=' ') file4.py:15: FutureWarning: Sorting 因为非连接轴未对齐。一个未来的版本 pandas 将默认更改为不排序。 要接受未来的行为,请传递 'sort=False'。 要保留当前行为并使警告静音,请通过 '排序=真'。 结果 = pd.concat([result,df],axis=1)

所以,我尝试按照建议添加 engine='python':

import glob
import pandas as pd
import numpy as np
engine='python'
files = glob.glob('*.txt')
result = pd.DataFrame()
for file in files:
    df = pd.read_csv(file,delimiter='  ')
    current_col = df.columns[0]
    df.reset_index(inplace=True)
    df.set_index(current_col,inplace=True)
    df.index.name = 'index'
    df.rename(columns={'index':current_col}, inplace=True)
    result = pd.concat([result,df],axis=1)

但这并没有解决问题。而且我在以前的帖子中没有找到补救措施(在不同的 python 版本上运行等)。有没有人有任何其他建议? 感谢您的帮助!

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    我尝试在加载 csv 时给出 engine='python' 参数

    df = pd.read_csv("file.csv", sep = "    ", engine='python')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-08
      • 2021-02-02
      • 1970-01-01
      • 2020-07-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多