Dictionary question in Python list.

Asked 2 years ago, Updated 2 years ago, 61 views

When there are various dictionaries in the list, I wonder how to extract them and how to apply them.

For example,

x = [{'company' : 'Nexon' , 'pay': 300}, {'company' : 'Kakao' , 'pay': 200}, {'company' : 'SK' , 'pay': 100}]

I want to know how to apply Nexon's value and Kakao's value separately to the repeat statement.

list dictionary python

2022-09-20 20:01

1 Answers

There are many ways, but it would be convenient to convert this into a Pandas data frame and do various operations.

Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> import pandas as pd
>>> x = [{'company' : 'Nexon' , 'pay': 300}, {'company' : 'Kakao' , 'pay': 200}, {'company' : 'SK' , 'pay': 100}]
>>> df = pd.DataFrame(x)
>>> df
  company  pay
0   Nexon  300
1   Kakao  200
2      SK  100
>>> print(df.to_markdown())
|    |    | company   |   pay |
|---:|:----------|------:|
|  |  0 | Nexon     |   300 |
|  |  1 | Kakao     |   200 |
|  |  2 | SK        |   100 |
>>> df.pay.sum()
600
>>> df.pay.mean()
200.0

>>> for r in df.itertuples():
    print(r)


Pandas(Index=0, company='Nexon', pay=300)
Pandas(Index=1, company='Kakao', pay=200)
Pandas(Index=2, company='SK', pay=100)
>>> for r in df.itertuples():
    print(r.company, r.pay)


Nexon 300
Kakao 200
SK 100
>>> 


2022-09-20 20:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.