简体   繁体   English

如何正确地将日期时间设置为 Pandas dataframe 的索引?

[英]How do I properly set the Datetime as an index for a Pandas dataframe?

I have a dataframe like below.我有一个 dataframe 如下所示。

|items(index)|event|day|
|---- |------|-----|
|1000|[buy, buy]|[2019-03-26, 2019-04-26]|
|1001|[buy, sell]|[2019-05-26, 2019-06-26]|
|1002|[buy, sell]|[2019-04-21, 2019-04-29]|
|1003|[sell, buy]|[2019-03-21, 2019-04-23]|

The order of values in the list of event columns corresponds to the order of values in the list of day columns;事件列列表中的值顺序对应于日期列列表中的值顺序; thus, 1000's first buy action occurred 2019-03-26.因此,1000 的首次买入动作发生在 2019 年 3 月 26 日。

The problem is that I want the Datetime to be the index and the current index to be the column, so it looks like the following.问题是我希望 Datetime 是索引,当前索引是列,所以它看起来像下面这样。

|day(index)|1000|1001|1002|1003|
|----|------|----- |-----|-----|
|2019-03-21|   |   |     |sell |
|2019-03-26|buy|   |     |     |
|2019-04-21|   |   |buy  |     |
|2019-04-23|   |   |     |     |
|2019-04-26|buy|   |     |     |
|2019-04-29|   |   |sell |     |
|2019-05-26|   |buy|     |     |
|2019-06-26|   |sell|    |     |

I don't know how I can effectively convert the above dataframe.我不知道我如何才能有效地转换上面的dataframe。

Assuming your df to be:假设您的 df 为:

In [354]: df = pd.DataFrame({'items':[1000, 1001, 1002, 1003], 'event':[['buy', 'buy'], ['buy', 'sell'], ['buy', 'sell'], ['sell', 'buy']], 'day':[['2019-03-26', '2019-04-26'], ['2019-05-26', '2019-06-26'], ['2019-04-21',
     ...: '2019-04-29'], ['2019-03-21', '2019-04-23']]})

In [355]: df
Out[355]: 
   items        event                       day
0   1000   [buy, buy]  [2019-03-26, 2019-04-26]
1   1001  [buy, sell]  [2019-05-26, 2019-06-26]
2   1002  [buy, sell]  [2019-04-21, 2019-04-29]
3   1003  [sell, buy]  [2019-03-21, 2019-04-23]

Use df.apply with df.pivot and df.fillna :df.applydf.pivotdf.fillna一起使用:

In [357]: x = df.set_index('items').apply(pd.Series.explode).reset_index()
In [360]: x.pivot('day', 'items', 'event').fillna('')
Out[360]: 
items      1000  1001  1002  1003
day                              
2019-03-21                   sell
2019-03-26  buy                  
2019-04-21              buy      
2019-04-23                    buy
2019-04-26  buy                  
2019-04-29             sell      
2019-05-26        buy            
2019-06-26       sell  

IIUC, try this: IIUC,试试这个:

import pandas as pd

df = pd.DataFrame({'items':[1000,1001,1002,1003],
                   'event':[['buy', 'buy'],
                           ['buy', 'sell'],
                           ['buy', 'sell'],
                        ['sell', 'buy']],
                  'day':[['2019-03-26', '2019-04-26'],
                          ['2019-05-26', '2019-06-26'],
                          ['2019-04-21',  '2019-04-39'],
                          ['2019-03-21', '2019-04-23']]})

dfe = df.explode(['event', 'day'])
dfe.pivot('day', 'items', 'event').fillna('')

Output: Output:

items      1000  1001  1002  1003
day                              
2019-03-21                   sell
2019-03-26  buy                  
2019-04-21              buy      
2019-04-23                    buy
2019-04-26  buy                  
2019-04-39             sell      
2019-05-26        buy            
2019-06-26       sell            

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM