按时间点随时间拆分多个对象的csv文件 [英] Splitting csv file of multiple objects over time by time-point

查看:120
本文介绍了按时间点随时间拆分多个对象的csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我有一个示例文件,其中包含多个对象,每个对象在相同的时间点进行测量(而且ND.T表示每个唯一的时间点).我想将此文件拆分为单独的文件(使用python脚本),其中包含每个时间点唯一的所有对象,而每个时间点仍包含标题.

Here I have an example file of multiple objects each measured at the same time-points (also ND.T represents each unique time point). I would like to split this file into separate files (using a python script) containing all objects unique to each time-point still containing the header.

原始文件:

ID  ND.T    Time [s]    Position X [%s] Position Y [%s] Speed [%s]  Area [%s]   Width [%s]  MeanIntensity
1   1        3.87         417.57          11.46          0.06        339.48       14.1          245.65
1   2        8.72         417.37          11.68          0.04        342.61       14.15         239.34
1   3        13.39        417.57          11.66          0.04        344.17       14.3          239.48
2   1        3.87         439.01           6.59          0.02        342.61       11.66         204.47
2   2        8.72         438.97           6.65          0.007       342.61       10.7          197.96
2   3        13.39        438.94           6.66          0.03        345.74       11.03         214.74

Time_3.87.csv

Time_3.87.csv

ID  ND.T    Time [s]    Position X [%s] Position Y [%s] Speed [%s]  Area [%s]   Width [%s]  MeanIntensity
1   1        3.87         417.57          11.46          0.06        339.48       14.1          245.65
2   1        3.87         439.01           6.59          0.02        342.61       11.66         204.47

Time_8.72.csv

Time_8.72.csv

ID  ND.T    Time [s]    Position X [%s] Position Y [%s] Speed [%s]  Area [%s]   Width [%s]  MeanIntensity
1   2        8.72         417.37          11.68          0.04        342.61       14.15         239.34
2   2        8.72         438.97           6.65          0.007       342.61       10.7          197.96

Time_13.39.csv

Time_13.39.csv

ID  ND.T    Time [s]    Position X [%s] Position Y [%s] Speed [%s]  Area [%s]   Width [%s]  MeanIntensity
1   3        13.39        417.57          11.66          0.04        344.17       14.3          239.48
2   3        13.39        438.94           6.66          0.03        345.74       11.03         214.74

示例2:

ID  ND.T    Time [s]    Position X [%s] Position Y [%s] Speed [%s]  Area [%s]   Width [%s]  MeanIntensity
1   1   3.87    417.57  11.46   0.06    339.48  14.1    245.65
1   2   8.72    417.37  11.68   0.04    342.61  14.15   239.34
1   3   13.39   417.57  11.66   0.04    344.17  14.3    239.48
1   4   18.1    417.73  11.71   0.04    337.92  14.14   225.17
1   5   22.81   417.83  11.89   0.03    344.17  14.64   233.3
1   6   27.48   417.69  11.83   0.02    345.74  14.23   238
1   7   32.16   417.65  11.94   0.03    345.74  14.71   230.75
2   1   3.87    439.01  6.59    0.02    342.61  11.66   204.47
2   2   8.72    438.97  6.65    0.007   342.61  10.7    197.96
2   3   13.39   438.94  6.66    0.03    345.74  11.03   214.74
2   4   18.1    438.9   6.53    0.04    342.61  10.46   202.9
2   5   22.81   438.97  6.7 0.02    342.61  10.3    194.32
2   6   27.48   438.89  6.71    0.006   350.43  11  219.41
2   7   32.16   438.87  6.74    0.05    348.87  10.36   219.58

推荐答案

您可以使用 pandas 来实现这一目标:

You can use pandas to achieve this:

import pandas as pd
df = pd.read_csv(your_file)
df.groupby('Time [s]').apply(lambda x: x.to_csv(str(x.name) + '.csv'))

以上内容将使用 read_csv ,然后在时间[s]"列上进行分组,并使用它来命名文件

The above will load your csv using read_csv and then group on the Time [s] column and use this to name the file

您可以看到df在时间[s]上分组:

You can see that the df is grouped on the Time [s]:

In [108]:
df.groupby('Time [s]').apply(lambda x: print(x))
   ID  ND.T  Time [s]  Position X [%s]  Position Y [%s]  Speed [%s]  \
0   1     1      3.87           417.57            11.46        0.06   
3   2     1      3.87           439.01             6.59        0.02   

   Area [%s]  Width [%s]  MeanIntensity  
0     339.48       14.10         245.65  
3     342.61       11.66         204.47  
   ID  ND.T  Time [s]  Position X [%s]  Position Y [%s]  Speed [%s]  \
0   1     1      3.87           417.57            11.46        0.06   
3   2     1      3.87           439.01             6.59        0.02   

   Area [%s]  Width [%s]  MeanIntensity  
0     339.48       14.10         245.65  
3     342.61       11.66         204.47  
   ID  ND.T  Time [s]  Position X [%s]  Position Y [%s]  Speed [%s]  \
1   1     2      8.72           417.37            11.68       0.040   
4   2     2      8.72           438.97             6.65       0.007   

   Area [%s]  Width [%s]  MeanIntensity  
1     342.61       14.15         239.34  
4     342.61       10.70         197.96  
   ID  ND.T  Time [s]  Position X [%s]  Position Y [%s]  Speed [%s]  \
2   1     3     13.39           417.57            11.66        0.04   
5   2     3     13.39           438.94             6.66        0.03   

   Area [%s]  Width [%s]  MeanIntensity  
2     344.17       14.30         239.48  
5     345.74       11.03         214.74  

Out[108]:
Empty DataFrame
Columns: []
Index: []

此处 groupby 将在时间[s]"列上分组,然后我们将

Here groupby will group on 'Time [s]' column, we then call apply to apply a lambda where we call the method to_csv on each grouping, we can access the group name using name attribute which is of dtype int so we cast to str and construct our csv name:

In [109]:
df.groupby('Time [s]').apply(lambda x: print(str(x.name) + '.csv'))

3.87.csv
8.72.csv
13.39.csv
Out[109]:
Empty DataFrame
Columns: []
Index: []

这篇关于按时间点随时间拆分多个对象的csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆