pandas 将字典转换为数据框时如何设置索引?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41207014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:38:17  来源:igfitidea点击:

How can I set index while converting dictionary to dataframe?

pythonpandasdictionary

提问by maynull

I have a dictionary that looks like the below

我有一本看起来像下面的字典

defaultdict(list,
        {'Open': ['47.47', '47.46', '47.38', ...],
         'Close': ['47.48', '47.45', '47.40', ...],
         'Date': ['2016/11/22 07:00:00', '2016/11/22 06:59:00','2016/11/22 06:58:00', ...]})

My purpose is to convert this dictionary to a dataframe and to set the 'Date' key values as the index of the dataframe.

我的目的是将此字典转换为数据框并将“日期”键值设置为数据框的索引。

I can do this job by the below commands

我可以通过以下命令完成这项工作

df = pd.DataFrame(dictionary, columns=['Date', 'Open', 'Close'])

     0  Date                  Open    Close
     1  2016/11/22 07:00:00   47.47   47.48
     2  2016/11/22 06:59:00   47.46   47.45
     3  2016/11/22 06:58:00   47.38   47.38

df.index = df.Date

     Date                  Date                  Open    Close
     2016/11/22 07:00:00   2016/11/22 07:00:00   47.47   47.48
     2016/11/22 06:59:00   2016/11/22 06:59:00   47.46   47.45
     2016/11/22 06:58:00   2016/11/22 06:58:00   47.38   47.38

but, then I have two 'Date' columns, one of which is the index and the other of which is the original column.

但是,然后我有两个“日期”列,其中一个是索引,另一个是原始列。

Is there any way to set index whileconverting dictionary to dataframe, without having overlapping columns like the below?

有没有办法将字典转换为数据帧时设置索引,而不会像下面这样重叠列?

     Date                  Close       Open
     2016/11/22 07:00:00   47.48       47.47
     2016/11/22 06:59:00   47.45       47.46
     2016/11/22 06:58:00   47.38       47.38

Thank you for reading this! :)

谢谢您阅读此篇!:)

回答by jezrael

Use set_index:

使用set_index

df = pd.DataFrame(dictionary, columns=['Date', 'Open', 'Close'])  
df = df.set_index('Date')       
print (df)
                      Open  Close
Date                             
2016/11/22 07:00:00  47.47  47.48
2016/11/22 06:59:00  47.46  47.45
2016/11/22 06:58:00  47.38  47.40

Or use inplace:

或使用inplace

df = pd.DataFrame(dictionary, columns=['Date', 'Open', 'Close'])  
df.set_index('Date', inplace=True)       
print (df)
                      Open  Close
Date                             
2016/11/22 07:00:00  47.47  47.48
2016/11/22 06:59:00  47.46  47.45
2016/11/22 06:58:00  47.38  47.40

Another possible solution filter out dictby Datekey and then set index by dictionary['Date']:

另一种可能的解决方案dictDate按键过滤,然后按dictionary['Date']以下方式设置索引:

df = pd.DataFrame({k: v for k, v in dictionary.items() if not k == 'Date'}, 
                   index=dictionary['Date'], 
                   columns=['Open','Close'])  
df.index.name = 'Date'
print (df)
                      Open  Close
Date                             
2016/11/22 07:00:00  47.47  47.48
2016/11/22 06:59:00  47.46  47.45
2016/11/22 06:58:00  47.38  47.40