0

Here i don't understand how can i filter year and make every year data a new dataset from a single dataset. My Dataset is - Dataset

In this dataset, 2010 to 2020 (11 years) data is available.

And I need this dataset in this format- Output image. 2010 dataset

2011 Dataset

So My output should be look like this in format.

path = r"C:\Users\krishna gupta\Downloads\Diwali\temp"
files = os.listdir(path)
b=[]
for file in files:
    df = pd.read_csv(path+'\\'+file)
    df = df[df['SYMBOL'] == 'NIFTY']
    df = df[df['INSTRUMENT'] == 'FUTIDX']
    df['RANGE'] = df['HIGH'] - df['LOW']
    df['YEAR'] = df.TIMESTAMP.str[-4:]
#     df = (df.iloc[0])
#     display(df)
    b.append(df)
df = pd.concat(b)
df = df.sort_values(['YEAR','TIMESTAMP'])
df.reset_index(inplace=True)
del df['index']
df = df[df['YEAR'] == '2011']
df.head(5)

This is my code, And df = df[df['YEAR'] == '2011'] here I can filter data this way, but I want to print the data frame by itself.

And I'm really sorry in advance if I'm not writing my question in the right format because I'm new on StackOverflow. and thanks in advance.

Krishna Gupta
  • 166
  • 1
  • 10
  • You could get a dictionary of dataframes keyed to year. `d = dict(tuple(df.groupby('YEAR')))` Then each DataFrame could be accessed like `d['2010']` or `d['2011']`. – Henry Ecker Oct 24 '21 at 05:20
  • Was that the kind of thing you meant? Because if that solves the problem I'll close this as a duplicate of [Split pandas dataframe based on groupby](https://stackoverflow.com/q/23691133/15497888) – Henry Ecker Oct 24 '21 at 05:30
  • 1
    @HenryEcker Yes. – Krishna Gupta Oct 24 '21 at 05:31

0 Answers0