【问题标题】:FIlrer csv table to have just 2 columns. Python pandas pd .pd过滤 csv 表,使其只有 2 列。 Python 熊猫 pd .pd
【发布时间】:2022-12-06 09:05:28
【问题描述】:

我得到了 .csv 文件,其中包含这样的行:

result,table,_start,_stop,_time,_value,_field,_measurement,device
,0,2022-10-23T08:22:04.124457277Z,2022-11-22T08:22:04.124457277Z,2022-10-24T12:12:35Z,44.61,power,shellies,Shelly_Kitchen-C_CoffeMachine/relay/0
,0,2022-10-23T08:22:04.124457277Z,2022-11-22T08:22:04.124457277Z,2022-10-24T12:12:40Z,17.33,power,shellies,Shelly_Kitchen-C_CoffeMachine/relay/0
,0,2022-10-23T08:22:04.124457277Z,2022-11-22T08:22:04.124457277Z,2022-10-24T12:12:45Z,41.2,power,shellies,Shelly_Kitchen-C_CoffeMachine/relay/0
,0,2022-10-23T08:22:04.124457277Z,2022-11-22T08:22:04.124457277Z,2022-10-24T12:12:51Z,33.49,power,shellies,Shelly_Kitchen-C_CoffeMachine/relay/0
,0,2022-10-23T08:22:04.124457277Z,2022-11-22T08:22:04.124457277Z,2022-10-24T12:12:56Z,55.68,power,shellies,Shelly_Kitchen-C_CoffeMachine/relay/0
,0,2022-10-23T08:22:04.124457277Z,2022-11-22T08:22:04.124457277Z,2022-10-24T12:12:57Z,55.68,power,shellies,Shelly_Kitchen-C_CoffeMachine/relay/0
,0,2022-10-23T08:22:04.124457277Z,2022-11-22T08:22:04.124457277Z,2022-10-24T12:13:02Z,25.92,power,shellies,Shelly_Kitchen-C_CoffeMachine/relay/0
,0,2022-10-23T08:22:04.124457277Z,2022-11-22T08:22:04.124457277Z,2022-10-24T12:13:08Z,5.71,power,shellies,Shelly_Kitchen-C_CoffeMachine/relay/0

我需要让它们看起来像这样:

                   time  value
0  2022-10-24T12:12:35Z  44.61
1  2022-10-24T12:12:40Z  17.33
2  2022-10-24T12:12:45Z  41.20
3  2022-10-24T12:12:51Z  33.49
4  2022-10-24T12:12:56Z  55.68

我的异常检测代码需要它,这样我就不必手动删除列等。至少不是全部。我无法使用与收集瓦数信息的机器配合使用的程序来做到这一点。 我试过了,但它不够用:

df = pd.read_csv('coffee_machine_2022-11-22_09_22_influxdb_data.csv')
df['_time'] = pd.to_datetime(df['_time'], format='%Y-%m-%dT%H:%M:%SZ')
df = pd.pivot(df, index = '_time', columns = '_field', values = '_value')
df.interpolate(method='linear') # not neccesary

它给出了这个输出:

            0
9      83.908
10     80.342
11     79.178
12     75.621
13     72.826
...       ...
73522  10.726
73523   5.241

【问题讨论】:

    标签: python pandas csv anomaly-detection isolation-forest


    【解决方案1】:

    这是向下投影到 pandas 生态系统中列子集的规范方法。

    df = df[['_time', '_value']]
    

    【讨论】:

      【解决方案2】:

      以下是如何执行此操作的示例:

      import pandas as pd
      
      # Read the CSV file into a pandas DataFrame
      df = pd.read_csv('coffee_machine_2022-11-22_09_22_influxdb_data.csv')
      
      # Convert the _time column to a datetime type
      df['_time'] = pd.to_datetime(df['_time'], format='%Y-%m-%dT%H:%M:%SZ')
      
      # Drop the columns that you don't need
      df = df.drop(columns=['result', 'table', '_start', '_stop', '_field', '_measurement', 'device'])
      
      # Set the index of the DataFrame to the _time column
      df = df.set_index('_time')
      
      # Print the resulting DataFrame
      print(df)
      

      此代码将 CSV 文件读入 pandas DataFrame,将 _time 列转换为 datetime 类型,删除不需要的列,将 DataFrame 的索引设置为 _time 列,然后打印生成的 DataFrame。

      您可以修改此代码以满足您的特定需要和要求。例如,您可以使用 interpolate 方法来填充 _value 列中的缺失值,或者您可以使用 resample 方法将数据重新采样到不同的时间间隔。

      【讨论】:

        猜你喜欢
        • 2020-10-03
        • 2020-04-13
        • 1970-01-01
        • 2021-09-06
        • 2019-05-12
        • 2019-01-15
        • 2018-12-10
        • 2019-08-01
        • 2020-09-30
        相关资源
        最近更新 更多