2016-11-14 80 views
1

我有以下數據框:Python:如何避免循環轉換爲熊貓數據框?

df: 
     y  m d val 
0 2013 10 1 33.5 
1 2013 10 2 37.1 
2 2013 10 3 25.9 
3 2013 10 4 31.3 
4 2013 10 5 35.3 
5 2013 10 6 55.4 
6 2013 10 7 29.5 
7 2013 10 8 31.3 
8 2013 10 9 27.7 
9 2013 10 10 25.9 

其中ymd分別對應於年,月,日。我想彙總它們並轉換爲datetime

df['date'] = 0 
for v in df.index: 
    df['date'][v] = datetime.datetime(df.y[v], df.m[v], df.d[v]) 

我想知道這是爲了避免循環

+0

我認爲適用是你的朋友:http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.apply.html#pandas.DataFrame.apply – Oleg

回答

1

這裏最好的辦法是一個辦法:

pd.to_datetime((df['y']*10000 + df['m']*100 + df['d']).astype(str)) 
Out: 
0 2013-10-01 
1 2013-10-02 
2 2013-10-03 
3 2013-10-04 
4 2013-10-05 
5 2013-10-06 
6 2013-10-07 
7 2013-10-08 
8 2013-10-09 
9 2013-10-10 
dtype: datetime64[ns] 
4

docstring

Assembling a datetime from multiple columns of a DataFrame. The keys can be 
common abbreviations like ['year', 'month', 'day', 'minute', 'second', 
'ms', 'us', 'ns']) or plurals of the same 

>>> df = pd.DataFrame({'year': [2015, 2016], 
         'month': [2, 3], 
         'day': [4, 5]}) 
>>> pd.to_datetime(df) 
0 2015-02-04 
1 2016-03-05 
dtype: datetime64[ns] 

代碼:

In [135]: pd.to_datetime(df.rename(columns={'y':'Year','m':'Month','d':'Day'}).iloc[:, :3]) 
Out[135]: 
0 2013-10-01 
1 2013-10-02 
2 2013-10-03 
3 2013-10-04 
4 2013-10-05 
5 2013-10-06 
6 2013-10-07 
7 2013-10-08 
8 2013-10-09 
9 2013-10-10 
dtype: datetime64[ns] 
相關問題