2016-08-08 5 views
1

現在、毎月のif条件に基づいてforループを使用して、その数値に相当する月に月を割り当てます。それは実行時にはかなり効率的だと思われますが、私の好みにはあまりにも手作業で醜いです。Python/Pandasで月数を数値に換算するにはどうすればいいですか?

これはどのようにしてよりうまく実行できますか?何とかして複数のif条件を単純化/凝縮することによって、また日付変換のために作られたある種のトランスレータを使用することで、それを改善することは可能でしょうか?どちらがお勧めですか?

#make numeric month 

combined = combined.sort_values('month') 
combined.index = range(len(combined)) 
combined['month_numeric'] = None 

for i in combined['month'].unique(): 
    first = combined['month'].searchsorted(i, side='left') 
    last = combined['month'].searchsorted(i, side='right') 
    first_num = list(first)[0] #gives first instance 
    last_num = list(last)[0] #gives last instance 
    if i == 'January': 
     combined['month_numeric'][first_num:last_num] = "01" 
    elif i == 'February': 
     combined['month_numeric'][first_num:last_num] = "02" 
    elif i == 'March': 
     combined['month_numeric'][first_num:last_num] = "03" 
    elif i == 'April': 
     combined['month_numeric'][first_num:last_num] = "04" 
    elif i == 'May': 
     combined['month_numeric'][first_num:last_num] = "05" 
    elif i == 'June': 
     combined['month_numeric'][first_num:last_num] = "06" 
    elif i == 'July': 
     combined['month_numeric'][first_num:last_num] = "07" 
    elif i == 'August': 
     combined['month_numeric'][first_num:last_num] = "08" 
    elif i == 'September': 
     combined['month_numeric'][first_num:last_num] = "09" 
    elif i == 'October': 
     combined['month_numeric'][first_num:last_num] = "10" 
    elif i == 'November': 
     combined['month_numeric'][first_num:last_num] = "11" 
    elif i == 'December': 
     combined['month_numeric'][first_num:last_num] = "12" 

答えて

4

あなたは、to_datetime、その後、monthを使用し、文字列に変換して使用することができますzfill

print (pd.to_datetime(df['month'], format='%B').dt.month.astype(str).str.zfill(2)) 

サンプル:

import pandas as pd 

df = pd.DataFrame({ 'month': ['January','February', 'December']}) 
print (df) 
     month 
0 January 
1 February 
2 December 

print (pd.to_datetime(df['month'], format='%B').dt.month.astype(str).str.zfill(2)) 
0 01 
1 02 
2 12 
Name: month, dtype: object 

別の解決策は、dictのdによってmapです210の

タイミング

df = pd.DataFrame({ 'month': ['January','February', 'December']}) 
print (df) 
df = pd.concat([df]*1000).reset_index(drop=True) 

print (pd.to_datetime(df['month'], format='%B').dt.month.astype(str).str.zfill(2)) 
print (df['month'].map({'January':'01','February':'02','December':'12'})) 

In [200]: %timeit (pd.to_datetime(df['month'], format='%B').dt.month.astype(str).str.zfill(2)) 
100 loops, best of 3: 13.5 ms per loop 

In [201]: %timeit (df['month'].map({'January':'01','February':'02','December':'12'})) 
1000 loops, best of 3: 462 µs per loop 
+0

これらのいずれもがソートされたデータを活用しているので、彼らは私が書いたものも遅いのオプションがありますか? – user1318135

+0

ソートは必要ないと思います。 – jezrael

+0

大きな混乱したデータセットでは、私のコード、またはこれらのソリューションのうちの1つが最も速くなるでしょうか?あるいは、違いが無視できると信じる理由はありますか? – user1318135

1

あなたはマップを使用することができます。

month2int = {"January":1, "February":2, ...} 
combined["month_numeric"] = combined["month"].map(month2int) 
+0

申し訳ありませんが、私の2番目の解決策を見てください。 – jezrael

関連する問題