简体   繁体   English

Python使用唯一日期列表作为索引创建新的数据框

[英]Python creating a new dataframe with list of unique dates as index

I have dateframe with index as df.index = 我有索引为df.index =的日期框架

2016-08-01 06:45:00    
2016-08-01 07:00:00    
2016-08-01 07:15:00    
.
.
2018-03-28 11:30:00    
2018-03-28 11:45:00    
2018-03-28 12:00:00  

I want to create a new dataframe that it will have only unique dates as 我想创建一个新的数据框,它将只有唯一的日期作为

new_df.index = new_df.index =

2016-08-01
2016-08-02
.
.
2018-03-28
2018-03-29

So, how to create a new dataframe with unique dates as index? 那么,如何创建一个以唯一日期作为索引的新数据框?

There is a build-in collection set in Python, that have only unique elements. 有一个内置的集合设置在Python,仅具有独特的元素。 You can do something like it: 您可以执行类似的操作:

new_data = sorted(list(set(old_data)))

If you want to crop out time in your datetime lines, you can modify this code with generators: 如果要在日期时间行中裁剪时间,可以使用生成器修改此代码:

new_data = sorted(list(set([elem[:10] for elem in old_data])))

Note, that if you have some info linked with your datetimes (like values in dict with datetime keys), you must handle elements removal before doing it. 请注意,如果您有一些与日期时间相关联的信息(例如带日期时间键的dict中的值),则必须先处理元素删除操作。

Since you have not provided any data I will assume it is not important. 由于您没有提供任何数据,因此我认为这并不重要。

Your first DataFrame appears to have a DatetimeIndex and it appears you want to convert it to a PeriodIndex . 您的第一个DataFrame似乎具有DatetimeIndex并且您似乎想将其转换为PeriodIndex You can do this to get the unique days with df.resample(rule='D').asfreq() . 您可以使用df.resample(rule='D').asfreq()来获得唯一日期。 Docs are here 文件在这里

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

相关问题 使用Python中的Dataframe索引创建包含数据的列表 - Creating a list with data from a Dataframe index in Python Python:在 dataframe 中获取唯一日期 - Python: take unique dates in dataframe python - pandas:使用关键字列表创建一个新的 dataframe - python - pandas: creating a new dataframe with a list of keywords 合并和内插2个dataframe列并从中创建唯一的数字列表 - python - combining and interpolating 2 dataframe column and creating unique number list from it Python:根据另一个数据框中的开始日期和结束日期在数据框中创建新列 - Python: Creating New Column in Dataframe based on Start and End Dates in Another Dataframe 在python中使用索引创建一个包含列表子集的新列表 - creating a new list with subset of list using index in python 按新的日期范围重新索引数据框 - Re-index dataframe by new range of dates 从另一列创建新列 + pandas dataframe 中的唯一数字索引 - Creating a new column from another column + unique numeric index in pandas dataframe 以最快的方式从 dataframe Python 中的索引创建一个新的字典列表 - Create a new list of dictionary from the index in dataframe Python with the fastest way Python Dataframe 从几百万行的大日期时间索引中提取唯一日期列表 - Python Dataframe extract list of unique dates from a big datetimeindex of few million rows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM