0

I have a csv file which has a large number of columns (20+). I want to filter the time column ("mins") so that it creates 60 separate dataframes which contains all the data for each minute. I can achieve it using this method below, but it is possible to do it using a for loop instead of repeating myself 60 times?

import pandas as pd

df = pd.read_csv("data.csv")

mins_0 = df[df['mins']==0]
mins_1 = df[df['mins']==1]
mins_2 = df[df['mins']==2]
mins_3 = df[df['mins']==3]
....
mins_59 = df[df['mins']==59]

martineau
  • 119,623
  • 25
  • 170
  • 301
Volgin
  • 1

2 Answers2

0

I still likes the pd.group_by in the comments better, but it is also doable by using np.unique() to find all unique values in the table and choose them in buckets.

import numpy as np
import pandas as pd

df = pd.read_csv("data.csv")

df_list = [df[df['mins']==x] for x in in.unique(df['mins'].values)]
aminrd
  • 4,300
  • 4
  • 23
  • 45
-1

As per Chris's comment, you may want to reconsider why you are doing this, but if you wanted to, this is a way you might go about it.

import pandas as pd

df = pd.read_csv('data.csv')

each_min = []

for i in range(60):
    each_min.append(df[df['mins'] == i])

this will put each dataframe in a list for you, you could also modify it to put them in a dictionary with each minute being the keys of the dictionary

CumminUp07
  • 1,936
  • 1
  • 8
  • 21