【问题标题】:Sort csv-data while reading, using pandas使用熊猫在阅读时对 csv 数据进行排序
【发布时间】:2015-03-26 07:26:06
【问题描述】:

我有一个包含如下条目的 csv 文件:

1,2014 1 1 0 1,5
2,2014 1 1 0 1,5
3,2014 1 1 0 1,5
4,2014 1 1 0 1,6
5,2014 1 1 0 1,6
6,2014 1 1 0 1,12
7,2014 1 1 0 1,17
8,2014 5 7 1 5,4

第一列是 ID,第二列是到达日期(最后输入示例:5 月 7 日凌晨 1:05),最后一列是工作持续时间(以分钟为单位)。

其实我是用 pandas 和下面的函数读入数据的:

import pandas as pd

def convert_data(csv_path):
    store = pd.HDFStore(data_file)
    print('Loading CSV File')
    df = pd.read_csv(csv_path, parse_dates=True)
    print('CSV File Loaded, Converting Dates/Times')
    df['Arrival_time'] = map(convert_time, df['Arrival_time'])
    df['Rel_time'] = (df['Arrival_time'] - REF.timestamp)/60.0
    print('Conversion Complete')
    store['orders'] = df

我的问题是:如何根据条目的持续时间对条目进行排序,但考虑到到达日期?所以,我想根据“到达日期+持续时间”对 csv 条目进行排序。这怎么可能?

感谢您的任何提示!最好的问候,斯坦。

【问题讨论】:

  • 不要使用from_csv,它不再维护,使用read_csv,你不需要使用你的方法转换时间只需将parse_dates=True传递给read_csv,如果dtypes是现在 datetimes 然后你可以添加它们然后使用它进行排序,你可以使用 timedelta 添加分钟
  • 好的,我同意这一点:pd.read_csv(csv_path, parse_dates=True) 但我不明白如何添加日期时间:S
  • 你应该可以做类似df['Arrival_date'] = df['Arrival_time'] + df['Duration'].apply(lambda x: datetime.timedelta(minutes=x)

标签: python sorting csv pandas


【解决方案1】:

好的,下面显示您可以转换日期时间,然后显示如何添加分钟:

In [79]:

df['Arrival_Date'] = pd.to_datetime(df['Arrival_Date'], format='%Y %m %d %H %M')
df
Out[79]:
   ID        Arrival_Date  Duration
0   1 2014-01-01 00:01:00         5
1   2 2014-01-01 00:01:00         5
2   3 2014-01-01 00:01:00         5
3   4 2014-01-01 00:01:00         6
4   5 2014-01-01 00:01:00         6
5   6 2014-01-01 00:01:00        12
6   7 2014-01-01 00:01:00        17
7   8 2014-05-07 01:05:00         4

In [80]:

import datetime as dt
df['Arrival_and_Duration'] = df['Arrival_Date'] + df['Duration'].apply(lambda x: dt.timedelta(minutes=int(x)))
df
Out[80]:
   ID        Arrival_Date  Duration Arrival_and_Duration
0   1 2014-01-01 00:01:00         5  2014-01-01 00:06:00
1   2 2014-01-01 00:01:00         5  2014-01-01 00:06:00
2   3 2014-01-01 00:01:00         5  2014-01-01 00:06:00
3   4 2014-01-01 00:01:00         6  2014-01-01 00:07:00
4   5 2014-01-01 00:01:00         6  2014-01-01 00:07:00
5   6 2014-01-01 00:01:00        12  2014-01-01 00:13:00
6   7 2014-01-01 00:01:00        17  2014-01-01 00:18:00
7   8 2014-05-07 01:05:00         4  2014-05-07 01:09:00

In [81]:

df.sort(columns=['Arrival_and_Duration'])
Out[81]:
   ID        Arrival_Date  Duration Arrival_and_Duration
0   1 2014-01-01 00:01:00         5  2014-01-01 00:06:00
1   2 2014-01-01 00:01:00         5  2014-01-01 00:06:00
2   3 2014-01-01 00:01:00         5  2014-01-01 00:06:00
3   4 2014-01-01 00:01:00         6  2014-01-01 00:07:00
4   5 2014-01-01 00:01:00         6  2014-01-01 00:07:00
5   6 2014-01-01 00:01:00        12  2014-01-01 00:13:00
6   7 2014-01-01 00:01:00        17  2014-01-01 00:18:00
7   8 2014-05-07 01:05:00         4  2014-05-07 01:09:00

【讨论】:

    猜你喜欢
    • 2021-10-08
    • 2019-04-12
    • 2014-09-19
    • 2012-05-16
    • 2021-08-08
    • 1970-01-01
    • 2014-12-29
    • 2015-05-09
    • 2021-04-13
    相关资源
    最近更新 更多