2017-05-30 3 views
1

私はfloat64としてインポートされたデータを持つデータフレームを持っています。私は、これまでに1列に変換することができましたが、私は複数のためにそれを拡張しようとすると、私が手:浮動小数点数から現在までの複数の列Pandas

ValueError: to assemble mappings requires at least that [year, month, day] be 
specified: [day,month,year] is missing 

これは、1列のために働く:

df['Col4'] = pd.to_datetime(df['Col4'].astype(str), format = '%Y%m%d') 

df.head().dtypes 
Out[151]: 
Col1   float64 
Col2   object 
Col3   float64 
Col4  datetime64[ns] 
Col5   float64 
Col6   float64 
dtype: object 

私は以下試しました複数の列のためと否定してしまった、任意のヘルプ感謝:

""" 
df[['Col4', 'Col5']] = pd.to_datetime(df[['Col4' , 'Col5']].astype(str), format = '%Y%m%d') 
df.head().dtypes 
""" 
ValueError: to assemble mappings requires at least that [year, month, day] be specified: [day,month,year] is missing 

答えて

1

使用apply

cols = ['Col4', 'Col5'] 
df[cols] = df[cols].apply(lambda x: pd.to_datetime(x.astype(str), format = '%Y%m%d')) 

サンプル:良いニュースは、あなたがこぶの上に私を得ている

df = pd.DataFrame({'Col4':[20150101.0, 20150102], 
        'Col5':[20160101.0, 20160102], 
        'Col1':[1,2]}) 
print (df) 
    Col1  Col4  Col5 
0  1 20150101.0 20160101.0 
1  2 20150102.0 20160102.0 

cols = ['Col4', 'Col5'] 
df[cols] = df[cols].apply(lambda x: pd.to_datetime(x.astype(str), format = '%Y%m%d')) 
print (df) 
    Col1  Col4  Col5 
0  1 2015-01-01 2016-01-01 
1  2 2015-01-02 2016-01-02 

cols = ['Col4', 'Col5'] 
df[cols] = df[cols].astype(str).apply(pd.to_datetime, format = '%Y%m%d') 
print (df) 
    Col1  Col4  Col5 
0  1 2015-01-01 2016-01-01 
1  2 2015-01-02 2016-01-02 
+0

...悪いニュースは、私が今圧延アップ私のベッドの時間を過ぎています。助けてくれてありがとう。 – RR33

+0

スーパー、それがうまくいくと思うよ;)おやすみ;) – jezrael

関連する問題