我正在使用 Pandas.read_excel 读取 Excel 文件。我想指定在应用于 x、y、z 列的日期转换器中使用的参数。读取数据时列名未知,但列索引已知。
示例:
def parse_day_first(x):
return dateutil.parser.parse(x, dayfirst=True)
pd.read_excel(filename, header=header, converters= {'Date': parse_day_first})
但是,在转换器字典中,需要指定列名称 - 这是未知的,因为文件可能有也可能没有标题。
有什么想法吗?
最佳答案
根据 documentation ,字典中的键可以是整数或列标签。
converters : dict, default None Dict of functions for converting values in certain columns. Keys can either be integers or column labels, values are functions that take one input argument, the Excel cell content, and return the transformed content.
如果日期是 Excel 文件中的第一列,让我们尝试一下:
pd.read_excel(filename, header=header, converters= {0: parse_day_first})
关于python - 在 Pandas.read_excel 中使用转换器时可以使用列索引吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44599009/