我如何 remove/strip Python 中变量的行号

How do I remove/strip the row number from a variable in Python

import csv
import pandas as pd
from datetime import datetime,time,date
from pandas.io.data import DataReader

fd = pd.read_csv('c:\path\to\file.csv')

fd.columns = ['Date','Time']

datex = fd.Date
timex = fd.Time

timestr = datetime.strptime ( str(datex+" "+timex) , "%m/%d/%Y %H:%M")

因此,我要做的是将日期和时间列传递给日期时间。有两列,日期和时间,显然包含日期和时间。但是当我尝试上述方法时,我收到此错误:

\n35760 08/07/2015 04:56\n35761 08/07/2015 04:57\n35762 08/07/2015 04:58\n35763 08/07/2015 04:59\ndtype: object' does not match format '%m/%d/%Y %H:%M'

那么,如何从 datex 和 timex 中剥离或删除 \nXXXXX?或者以其他方式匹配格式?

使用pandas内置parse_dates函数:)

pd.read_csv('c:\path\to\file.csv', parse_dates=True)
# concatenate two columns ( date and time ) into one column that represent date time now into one columns
datetime = datex + timex
# remove additional characters and spaces from newly created datetime colummn
datetime = datetime.str.replace('\n\d+' , '').str.strip()
# then string should be ready to be converted to datetime easily
pd.to_datetime(datetime , format='%m/%d/%Y%H:%M')