簡體   English   中英

將數據框與dict vallues一起使用時出現Pandas錯誤

[英]Pandas error while using dataframe with dict vallues

這是我嘗試過的和得到的:

start = datetime.datetime.strptime("1973-01-01", "%Y-%m-%d")
moving = start
d = {}
year =0
month =0
while moving < datetime.datetime(1982,7,1):
#     print moving
    if moving.year == year:
        if moving.month == month:
            if moving.weekday() < 5:
                d[str(moving.year)+"-"+ str(moving.month)] += 1
        else:
            d[str(moving.year)+"-"+ str(moving.month)]=0
            if moving.weekday() < 5:
                d[str(moving.year)+"-"+ str(moving.month)] += 1
    else:
#         d[moving.year] = {}
        d[str(moving.year)+"-"+ str(moving.month)]=0
        if moving.weekday() < 5:
            d[str(moving.year)+"-"+ str(moving.month)] += 1


    year = moving.year
    month = moving.month
    moving += datetime.timedelta(days=1)

print(d)

df_dow = pd.DataFrame(d, columns=['Month', 'DateValue'])
df_dow.Month = pd.to_datetime(df_dow.Month)
df_dow.sort('Month', inplace=True)

def holiday_adj(x):
    if x['Month'].month==1:
        x['DateValue'] -=1
        return x['DateValue'] 
    elif x['Month'].month==2:
        x['DateValue'] -=1
        return x['DateValue']
    elif x['Month'].month==5:
        x['DateValue'] -=1
        return x['DateValue']
    elif x['Month'].month==7:
        x['DateValue'] -=1
        return x['DateValue']
    elif x['Month'].month==9:
        x['DateValue'] -=1
        return x['DateValue']
    elif x['Month'].month==10:
        x['DateValue'] -=1
        return x['DateValue']    
    elif x['Month'].month==11:
        x['DateValue'] -=3   
        return x['DateValue']
    elif x['Month'].month==12:
        x['DateValue'] -=2
        return x['DateValue']

    else:
        return x['DateValue']

df_dow['days'] = df_dow.apply(holiday_adj, axis=1)
df_dow.set_index('Month', inplace=True)
df_dow.index.name = None

錯誤和輸出:

{'1976-5': 21, '1978-10': 22, '1974-11': 21, '1981-9': 22, '1976-10': 21, '1977-10': 21, '1979-10': 23, '1981-11': 21, '1974-9': 21, '1981-8': 21, '1975-10': 23, '1979-9': 20, '1977-7': 21, '1974-6': 20, '1975-2': 20, '1977-8': 23, '1981-10': 22, '1978-2': 20, '1980-2': 21, '1978-6': 22, '1977-5': 22, '1982-3': 23, '1980-11': 20, '1980-3': 21, '1982-4': 22, '1980-4': 22, '1973-12': 21, '1976-6': 22, '1982-5': 21, '1977-2': 20, '1982-6': 22, '1978-8': 23, '1980-1': 23, '1980-9': 22, '1979-3': 22, '1973-3': 22, '1977-1': 21, '1976-9': 22, '1980-12': 23, '1974-5': 23, '1979-12': 21, '1981-6': 22, '1973-2': 20, '1973-4': 21, '1978-11': 22, '1981-3': 22, '1975-1': 23, '1978-9': 21, '1978-5': 23, '1973-10': 23, '1975-11': 20, '1973-1': 23, '1977-4': 21, '1973-5': 23, '1979-11': 22, '1974-3': 21, '1976-4': 22, '1973-11': 22, '1976-1': 22, '1981-7': 23, '1974-4': 22, '1975-12': 23, '1978-3': 23, '1976-7': 22, '1976-11': 22, '1976-12': 23, '1978-4': 20, '1979-2': 20, '1974-12': 22, '1975-6': 21, '1981-4': 22, '1982-2': 20, '1979-5': 23, '1973-8': 23, '1974-1': 23, '1975-7': 23, '1980-10': 23, '1973-9': 20, '1981-2': 20, '1979-4': 21, '1981-1': 22, '1976-3': 23, '1975-5': 22, '1977-6': 22, '1976-2': 20, '1974-7': 23, '1982-1': 21, '1973-6': 21, '1979-7': 22, '1980-6': 21, '1980-5': 22, '1977-11': 22, '1980-7': 23, '1979-1': 23, '1981-12': 23, '1979-8': 23, '1973-7': 22, '1975-8': 21, '1975-9': 22, '1974-2': 20, '1978-1': 22, '1977-3': 23, '1977-12': 22, '1974-10': 23, '1977-9': 22, '1979-6': 21, '1981-5': 21, '1975-4': 22, '1978-7': 21, '1976-8': 22, '1975-3': 21, '1978-12': 21, '1974-8': 22, '1980-8': 21}
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-50-f4b80a158a0d> in <module>()
     30 print(d)
     31 
---> 32 df_dow = pd.DataFrame(d.items(), columns=['Month', 'DateValue'])
     33 df_dow.Month = pd.to_datetime(df_dow.Month)
     34 df_dow.sort('Month', inplace=True)

c:\python35\lib\site-packages\pandas\core\frame.py in __init__(self, data, index, columns, dtype, copy)
    402                                          dtype=values.dtype, copy=False)
    403             else:
--> 404                 raise ValueError('DataFrame constructor not properly called!')
    405 
    406         NDFrame.__init__(self, mgr, fastpath=True)

ValueError: DataFrame constructor not properly called!  

請讓我知道我是否錯過了什么?

我認為需要通過key s和value s創建DaatFrame

df = pd.DataFrame({'Month': list(d.keys()), 'DateValue':list(d.values())})

或者通過DataFrame.from_dictreset_index

df = pd.DataFrame.from_dict(d, orient='index').reset_index()
df.columns=['Month', 'DateValue']

print (df.head())
     Month  DateValue
0   1976-5         21
1  1978-10         22
2  1974-11         21
3   1981-9         22
4  1976-10         21

您還可以創建一個模板DataFrame,然后通過說出以下內容來分配新列:

df['Month'] = pd.Series(d.values)
df['DateValue'] = pd.Series(d.keys())
.
.
.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM