Splitting Csv File Of Multiple Objects Over Time By Time-point
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 f
Solution 1:
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'))
The above will load your csv using read_csv
and then group on the Time [s] column and use this to name the file
You can see that the df is grouped on the Time [s]:
In[108]:
df.groupby('Time [s]').apply(lambda x: print(x))
IDND.TTime[s]PositionX[%s]PositionY[%s]Speed[%s] \
0113.87417.5711.460.063213.87439.016.590.02Area[%s]Width[%s]MeanIntensity0339.4814.10245.653342.6111.66204.47IDND.TTime[s]PositionX[%s]PositionY[%s]Speed[%s] \
0113.87417.5711.460.063213.87439.016.590.02Area[%s]Width[%s]MeanIntensity0339.4814.10245.653342.6111.66204.47IDND.TTime[s]PositionX[%s]PositionY[%s]Speed[%s] \
1128.72417.3711.680.0404228.72438.976.650.007Area[%s]Width[%s]MeanIntensity1342.6114.15239.344342.6110.70197.96IDND.TTime[s]PositionX[%s]PositionY[%s]Speed[%s] \
21313.39417.5711.660.0452313.39438.946.660.03Area[%s]Width[%s]MeanIntensity2344.1714.30239.485345.7411.03214.74Out[108]:
EmptyDataFrameColumns: []Index: []
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: []
Post a Comment for "Splitting Csv File Of Multiple Objects Over Time By Time-point"