In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how Pandas converts DataFrame into a dictionary, which has a certain reference value. Interested friends can refer to it. I hope you can learn a lot after reading this article.
When using pandas, we know that pandas uses dataFrame to store data, but we are more likely to do dictionary operations on python than on dataFrame, so we need to convert dataFrame to a dictionary.
The to_dict () method can set the column name to the dictionary key, set the "ID" column as the index and then transpose the DataFrame, which is one way to do this. To_dict () also accepts a 'orient' parameter, which you need to output a list of values for each column. Otherwise, {index: value} returns the dictionary of the form for each column.
You can complete these steps using the following lines:
> > df.set_index ('ID'). T.to_dict (' list') {'packs: [1, 3, 2],' Q bands: [4, 3, 2], 'ritual: [4, 0, 9]}
If you need a different dictionary format, here is an example of possible oriental parameters. Consider the following simple DataFrame:
> df = pd.DataFrame ({'averse: [' red', 'yellow',' blue'], 'baked: [0.5,0.25,0.125]}) > df a b0 red 0.5001 yellow 0.2502 blue 0.125
Then the options are as follows.
Dict-default: the column name is the key and the value is the dictionary of the index: data pair
> df.to_dict ('dict') {' asides: {0: 'red', 1:' yellow', 2: 'blue'},' baked: {0: 0.5,1: 0.25,2: 0.125}}
List-the key is the column name and the value is the list of column data
> > df.to_dict ('list') {' asides: ['red',' yellow', 'blue'],' baked: [0.5,0.25,0.125]}
Series-such as' list', but the value is Series
> df.to_dict ('series') {' asides: 0 red 1 yellow 2 blue Name: a, dtype: object, 'baked: 0 0.500 1 0.250 2 0.125 Name: B, dtype: float64}
Split-split column / data / index into keys with values of column names and data values by row and index label
> > df.to_dict ('split') {' columns': ['asides,' b'], 'data': [[' red', 0.5], ['yellow', 0.125], [' blue', 0.125]], 'index': [0,1,2]}
Record-each row becomes a dictionary, where the key is the column name and the value is the data in the cell
> df.to_dict ('records') > [{' await: 'red',' baked: 0.5}, {'axed:' yellow', 'baked: 0.25}, {' axed: 'blue',' baked: 0.125}]
Index-similar to 'records', but a dictionary with keys as index tags (not lists)
> df.to_dict ('index') {0: {' averse: 'red',' baked: 0. 5}, 1: {'axed:' yellow', 'baked: 0.125}, 2: {' axed: 'blue',' baked: 0.125}} Thank you for reading this article carefully. I hope the article "how to convert DataFrame into a Dictionary" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel, and more related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.