简体   繁体   中英

Reading an excel file in pandas

I am reading an excel file into pandas, but I am getting the following:

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. Here is my pandas read command:

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:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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