0

I want to make a dictionary of data frames from existing data frame as shown in the picture

df2=pd.DataFrame({"Col":[9,8,0],"Row":[6,4,22],"id":[26,55,27]})

**CODE THAT I NEEDS MODIFICATION**

i={k:g["Col"].to_frame() for k,g in df2.groupby("id")}###### THIS IS MY TRIED CODE

    ##where df2 is the name of the pandas data frame   

I want Id to be the key of the dictionary of the data frame and the "Col" and "Row" to be as a data frame 
gmm005
  • 27
  • 5
  • 2
    Welcome to Stackoverflow! Make sure you read the guidelines for posting questions. As a rule of thumb, do not post screen shots of your data. Post data that can be used to reproduce your issue. – Serge de Gosson de Varennes Nov 22 '20 at 15:49
  • [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). [Discourage screenshots of code and/or errors](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors)...[Why not upload images of code on SO when asking a question?](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question) ... [You should not post code as an image because:...](https://meta.stackoverflow.com/a/285557/2823755) – wwii Nov 22 '20 at 15:56
  • I have added a sample code and removed the picture of the data frame and added a code to produce the data frame – gmm005 Nov 23 '20 at 11:33

2 Answers2

1

You could do this. Since you didn't share data I fixed a df of my own. For your case it works the same way.

    dates   lon lat city
0   20201211    53.12   102.45  45
1   20201211    53.13   102.46  45
2   20201211    53.14   102.47  45
3   20201211    53.15   102.48  34
4   20201211    53.16   102.49  34
5   20201211    53.17   102.50  34

and then using city as key:

df.set_index('city').to_dict()

which gives

{'dates ': {45: 20201211, 34: 20201211},
 'lon': {45: 53.14, 34: 53.17},
 'lat ': {45: 102.47, 34: 102.5}}
  • Hi thanks for the comment but i believe as mentioned in the question it produces a dictionary of dictionary i want a dictionary of the data frames as mentioned in the question – gmm005 Nov 23 '20 at 10:23
0

question is not presented correctly pls mention some code by which we can also reproduce the data frame

but the ans given below is based on my understanding of the question using similar case

df2=pd.DataFrame({"Col":[9,8,0],"Row":[6,4,22],"id":[26,55,27]})
df2
    Col Row id
0   9   6   26
1   8   4   55
2   0   22  27


D={}
df2=df2.set_index("id")
for x in df2.index:
  D[x]=df2.loc[x][["Row","Col"]] # any subsequent columns here

this works

gilf0yle
  • 1,092
  • 3
  • 9
  • ~{26: Row 6 Col 9 Name: 26, dtype: int64, 55: Row 4 Col 8 Name: 55, dtype: int64, 27: Row 22 Col 0 Name: 27, dtype: int64}~ This is the output of the code its not producing a dictionary of data frames as asked in the question – gmm005 Nov 23 '20 at 11:16
  • try printing dictionary D the produced result lies in dictionary D – gilf0yle Nov 23 '20 at 19:22
  • and do consider accepting if my solution if it works – gilf0yle Nov 23 '20 at 19:32
  • This is the output of D . Its not the intended output sorry {26: Row 6 Col 9 Name: 26, dtype: int64, 55: Row 4 Col 8 Name: 55, dtype: int64, 27: Row 22 Col 0 Name: 27, dtype: int64} – gmm005 Nov 23 '20 at 20:11