将多个字段拆分为一列

时间:2018-07-21 13:41:05

标签: python file dataframe

我有以下文字

04/20/2009; 04/20/09; 4/20/09; 4/3/09
Mar-20-2009; Mar 20, 2009; March 20, 2009; Mar. 20, 2009; Mar 20 2009;
20 Mar 2009; 20 March 2009; 20 Mar. 2009; 20 March, 2009
Mar 20th, 2009; Mar 21st, 2009; Mar 22nd, 2009
Feb 2009; Sep 2009; Oct 2010
6/2008; 12/2009
2009; 2010

我的想法是将所有日期替换为一种格式,但是我需要将所有日期设置为一个pd.Series列对象。

这是我到目前为止所做的。

with open('dates.txt') as file:
    for row in file:
        doc.append(line)

我想要类似

1 0          Mar-20-2009
  1         Mar 20, 2009
  2       March 20, 2009
  3        Mar. 20, 2009
  4          Mar 20 2009
2 0          20 Mar 2009
  1        20 March 2009
  2         20 Mar. 2009
  3       20 March, 2009
3 0       Mar 20th, 2009
  1       Mar 21st, 2009
  2       Mar 22nd, 2009
4 0             Feb 2009
  1             Sep 2009
  2             Oct 2010

1 个答案:

答案 0 :(得分:2)

要从该文本文件创建系列,请执行以下操作

with open(file) as f:
    txt = f.read().replace("\n","")
series = pd.Series(txt.split(";"))