抽出リストパンダシリーズの要素と日時に変換

研究天体物理学:

私は渡す午前シリーズは次のようになります。

qa_answers['date_of_birth']


1                 []
2                 []
...
2600    [1988/11/23]
2601     [1992/7/15]
2602    [1993/11/8"]
2603    [1997/08/31]
2604     [1971/2/11]
2605    [1979/11/1"]
2606     [1993/9/19]
2607    [1985/01/12]
2608    [1977/11/3"]
2609     [1981/7/2"]
2610     [1952/4/9"]
2611     [1991/8/20]
2612     [1993/1/31]
Name: date_of_birth, dtype: object

この問題は次の2つの部分で構成されます:

  1. 私は、日時にシリーズ(オブジェクト)の種類を変換したいです。
  2. 私はto_datetimeを使用しようとしたときしかし、私はこのエラーを得ました。
qa_answers['date_of_birth'] = pd.to_datetime(qa_answers['date_of_birth'],errors='coerce')

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-147-96dff0351764> in <module>()
     28 qa_answers['date_of_birth2']= qa_answers['answers'].str.findall(dob2)
     29 qa_answers['date_of_birth'] = qa_answers['date_of_birth1'] + qa_answers['date_of_birth2']
---> 30 qa_answers['date_of_birth'] = pd.to_datetime(qa_answers['date_of_birth'],errors='coerce')
     31 
     32 

4 frames
/usr/local/lib/python3.6/dist-packages/pandas/core/algorithms.py in unique(values)
    403 
    404     table = htable(len(values))
--> 405     uniques = table.unique(values)
    406     uniques = _reconstruct_data(uniques, dtype, original)
    407     return uniques

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.unique()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable._unique()

TypeError: unhashable type: 'list'

私が推測するので、私は、最初のリストから要素を抽出してみてください。どのように私はこの仕事を行うことができますか?

PSはまた、あなたが要素に「"」除去するためのいくつかのヒントを与えることができますか?

セルジュBallestaの:

あなたは、まず彼らの最初の要素に非空のリストを変換し、それをきれいにし、空の文字列に空のリストを変換する必要があります。

df.date_of_birth.apply(lambda x: x[0].replace('"', '') if len(x) > 0 else '')

提供します:

1                 
2 
...                
2600    1988/11/23
2601     1992/7/15
2602     1993/11/8
2603    1997/08/31
2604     1971/2/11
2605     1979/11/1
2606     1993/9/19
2607    1985/01/12
2608     1977/11/3
2609      1981/7/2
2610      1952/4/9
2611     1991/8/20
2612     1993/1/31

そして、あなたは簡単にdatetime型の列にそれを変換することができます:

pd.to_datetime(df.date_of_birth.apply(lambda x: x[0].replace('"', '') if len(x) > 0 else ''))

あなたが得ます:

1             NaT
2             NaT
2600   1988-11-23
2601   1992-07-15
2602   1993-11-08
2603   1997-08-31
2604   1971-02-11
2605   1979-11-01
2606   1993-09-19
2607   1985-01-12
2608   1977-11-03
2609   1981-07-02
2610   1952-04-09
2611   1991-08-20
2612   1993-01-31
Name: date_of_birth, dtype: datetime64[ns]

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=33773&siteId=1