Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a dict of list from pandas row?

Tags:

python

pandas

I have a weird problem. I have a index and bunch of columns in a dataframe. I want the index to be a key and all the other columns to be in a list. Here's a example

df:

    0   1   2   3
Barker Minerals Ltd             
Blackout Media Corp             
Booking Holdings Inc    Booking Holdings Inc    Booking Holdings Inc 4.10 04/13/2025    BOOKING HOLDINGS INC    
Baker Hughes Company    Baker Hughes Company    BAKER HUGHES A GE COMPANY LLC-3.34%-12-15-2027  BAKER HUGHES A GE COMPANY LLC-3.14%-11-7-2029   
Bank of Queensland Limited  Bank of Queensland Limited  Bank of Queensland Limited FRN 10-MAY-2026 3.50% 05/10/26   Bank of Queensland Limited FRN 26-OCT-2020 1.27% 10/26/20   Bank of Queensland Limited FRN 16-NOV-2021 1.12% 11/16/21

If I do this command it turns everything into a list when I want it to be a dict of a list:

df.to_numpy().tolist()

I want a dict with each key a list of values in the other columns(kind of like this):

{
Barker Minerals Ltd:    
Blackout Media Corp:    
Booking Holdings Inc:   [Booking Holdings Inc ,Booking Holdings Inc 4.10 04/13/2025,BOOKING HOLDINGS INC]
Baker Hughes Company:   [Baker Hughes Company ,BAKER HUGHES A GE COMPANY LLC-3.34%-12-15-2027,BAKER HUGHES A GE COMPANY LLC-3.14%-11-7-2029]
Bank of Queensland Limited: [Bank of Queensland Limited ,Bank of Queensland Limited FRN 10-MAY-2026 3.50% 05/10/26,Bank of Queensland Limited FRN 26-OCT-2020 1.27% 10/26/20, Bank of Queensland Limited FRN 16-NOV-2021 1.12% 11/16/21]
}

Is this possible to do?

like image 256
Lostsoul Avatar asked Dec 01 '25 09:12

Lostsoul


1 Answers

The easiest answer as pointed out in the comments by Michael Szczesny:

df.T.to_dict(orient="list")

The output:

{'Barker Minerals Ltd': [nan, nan, nan, nan],
 'Blackout Media Corp': [nan, nan, nan, nan],
 'Booking Holdings Inc': ['Booking Holdings Inc',
  'Booking Holdings Inc 4.10 04/13/2025',
  'BOOKING HOLDINGS INC',
  nan],
 'Baker Hughes Company': ['Baker Hughes Company',
  'BAKER HUGHES A GE COMPANY LLC-3.34%-12-15-2027',
  'BAKER HUGHES A GE COMPANY LLC-3.14%-11-7-2029',
  nan],
 'Bank of Queensland Limited': ['Bank of Queensland Limited',
  'Bank of Queensland Limited FRN 10-MAY-2026 3.50% 05/10/26',
  'Bank of Queensland Limited FRN 26-OCT-2020 1.27% 10/26/20',
  ' Bank of Queensland Limited FRN 16-NOV-2021 1.12% 11/16/21']}

Also, in case you would like to lose all the nans, then the code is as follows:

df =  pd.read_csv("df_to_dict.csv", index_col=0)
val = df.T.to_dict(orient="list")
cleaned_val = {}

for i in val:
    cleaned_val[i] = [j for j in val[i] if str(j)!="nan"]
    
cleaned_val

The output is as follows:

{'Barker Minerals Ltd': [],
 'Blackout Media Corp': [],
 'Booking Holdings Inc': ['Booking Holdings Inc',
  'Booking Holdings Inc 4.10 04/13/2025',
  'BOOKING HOLDINGS INC'],
 'Baker Hughes Company': ['Baker Hughes Company',
  'BAKER HUGHES A GE COMPANY LLC-3.34%-12-15-2027',
  'BAKER HUGHES A GE COMPANY LLC-3.14%-11-7-2029'],
 'Bank of Queensland Limited': ['Bank of Queensland Limited',
  'Bank of Queensland Limited FRN 10-MAY-2026 3.50% 05/10/26',
  'Bank of Queensland Limited FRN 26-OCT-2020 1.27% 10/26/20',
  ' Bank of Queensland Limited FRN 16-NOV-2021 1.12% 11/16/21']}

The documentation of to_dict() can be accessed here.

like image 176
sotmot Avatar answered Dec 03 '25 23:12

sotmot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!