简体   繁体   English

在熊猫中读取excel文件

[英]Reading an excel file in pandas

I am reading an excel file into pandas, but I am getting the following: 我正在将一个excel文件读入熊猫,但我得到以下内容:

Out[8]:
0        \tFLOOD LIGHTS\t
1        \tFLOOD LIGHTS\t
2        \tPAR 38 LIGHT\t
3                \tMILO\t
4    \tQ-12251-DO1 MILO\t

I do not want the "\\t" in my data. 我不想在我的数据中使用“\\ t”。 Here is my pandas read command: 这是我的pandas read命令:

import pandas as pd
data = pd.read_ex('/home/Desktop/sample.xlsx')

It seems you have trailing tabs in your data. 您的数据似乎有尾随选项卡。

So need strip for remove it: 所以需要strip它:

data['col'] = data['col'].str.strip()

If all columns: 如果所有列:

data = data.apply(lambda x: x.str.strip())

#then convert possible numeric columns
data['num_col'] = data['num_col'].astype(int)

Or if need remove \\t strings use replace with ^ for start of string and $ for end: 或者如果需要删除\\t字符串使用replace^用于字符串的开头而$用于结束:

data = data['col'].replace(['^\t', '\t$'], '', regex=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM