我有蟒蛇字典是這樣的:大熊貓數據框中更改列值
keysdict={'date': {20: '201012', 21: '201101', 22: '201102', 23: '201103', 24: '201104', 25: '201105', 26: '201106', 27: '201107', 28: '201108', 29: '201109', 30: '201110', 31: '201111'}}
而且我有熊貓據幀這樣
A B
20 0.123
21 2.123
22 4.45
23 3.2
24 3.1
25 3.9
26 4.2
現在我想添加一個額外的列C
這需要相應的值來自Column A
的字典鍵。像下面一個
A B C
20 0.123 201012
21 2.123 201101
22 4.45 201102
23 3.2 201103
24 3.1 201104
25 3.9 201105
26 4.2 201106
我想這
df.loc[:,'C']=keysdict['date'][df.loc['A']]
但是,得到下面的錯誤
KeyError: 'the label [A] is not in the [index]
仍然收到錯誤'KeyError:'A'' – curiousguy
我在答案中包含了一個工作示例。 – DyZ