Store only values in a specific location in the dictionary

Asked 2 years ago, Updated 2 years ago, 76 views

class Data(object):
    def __init__(self):
        self.name = list(rank.values())[0]


dataset = Data()

I want to extract only the first name from the dictionary named Rank in the picture and save it in the list, what should I do?

dictionary class python

2022-09-20 15:49

1 Answers

self.name = [ v[0] for v in rank.values()]
>>> import pandas as pd

>>> df = pd.DataFrame([["jeff", "188B", "us"],["elon", "170B", "us"]])
>>> df
      0     1   2
0  jeff  188B  us
1  elon  170B  us

>>> df = pd.DataFrame([["jeff", "188B", "us"],["elon", "170B", "us"]], columns=["name", "wealth", "nation"])
>>> df
   name wealth nation
0  jeff   188B     us
1  elon   170B     us
data = pd.DataFrame(rank.values(), 
                    columns=[ "name", "wealth", "wealth_delta1", "wealth_delta2", "nationality", "business area" ])


2022-09-20 15:49

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.