簡體   English   中英

如何以某種方式創建 pandas 數據框?

[英]How can I create a pandas data frame in a certain way?

我需要創建一個 pandas dataframe ,其中包含所有必需的信息,其中 dataframe 的每一行都應該是一個軌道。 我還需要將 dataframe 按流行度分數排序,這樣最受歡迎的曲目在頂部,最不受歡迎的曲目在底部。 我嘗試了很多方法,但沒有奏效。 非常感謝您的幫助。

我正在分享我的嵌套字典。

{'Artist name': ['Paramore', 'Weezer', 'Lizzo'],
 'Track name': (['Still into You',
   "Ain't It Fun",
   'Hard Times',
   'Misery Business',
   'The Only Exception',
   'Ignorance',
   'Rose-Colored Boy',
   'Fake Happy',
   "That's What You Get",
   'Brick by Boring Brick'],
  ['Island In The Sun',
   "Say It Ain't So",
   'Buddy Holly',
   'Beverly Hills',
   'Africa',
   'The End of the Game',
   'Hash Pipe',
   'Undone - The Sweater Song',
   'My Name Is Jonas',
   'Take On Me'],
  ['Truth Hurts',
   'Good As Hell',
   'Good As Hell (feat. Ariana Grande) - Remix',
   'Juice',
   'Boys',
   'Tempo (feat. Missy Elliott)',
   'Blame It on Your Love (feat. Lizzo)',
   'Soulmate',
   'Water Me',
   'Like A Girl']),
 'Release date': (['2013-04-05',
   '2013-04-05',
   '2017-05-12',
   '2007-06-11',
   '2009-09-28',
   '2009-09-28',
   '2017-05-12',
   '2017-05-12',
   '2007-06-11',
   '2009-09-28'],
  ['2001-05-15',
   '1994-05-10',
   '1994-05-10',
   '2005-05-10',
   '2019-01-24',
   '2019-09-10',
   '2001-05-15',
   '1994-05-10',
   '1994-05-10',
   '2019-01-24'],
  ['2019-05-03',
   '2016-03-09',
   '2019-10-25',
   '2019-04-19',
   '2019-04-18',
   '2019-04-19',
   '2019-09-13',
   '2019-04-19',
   '2019-04-18',
   '2019-04-19']),
 'Popularity score': ([76, 74, 73, 73, 72, 69, 66, 66, 65, 65],
  [77, 75, 73, 71, 67, 67, 66, 65, 63, 62],
  [94, 90, 86, 84, 72, 78, 68, 72, 58, 71])}

肯定有更有效的方法,但這里有一個解決方案

import pandas as pd

def gen_artist_frame(d):
    categories = [c for c in d.keys()]

    for idx, artist in enumerate(d['Artist name']):

        artist_mat = [d[j][idx] for j in categories[1:]]

        artist_frame = pd.DataFrame(artist_mat, index=categories[1:]).T

        artist_frame[categories[0]] = artist

        yield artist_frame

def collapse_nested_artist(d):
    return pd.concat([
        a for a in gen_artist_frame(d)
        ])

d = {'Artist name': ['Paramore', 'Weezer', 'Lizzo'],
 'Track name': (['Still into You',
   "Ain't It Fun",
   'Hard Times',
   'Misery Business',
   'The Only Exception',
   'Ignorance',
   'Rose-Colored Boy',
   'Fake Happy',
   "That's What You Get",
   'Brick by Boring Brick'],
  ['Island In The Sun',
   "Say It Ain't So",
   'Buddy Holly',
   'Beverly Hills',
   'Africa',
   'The End of the Game',
   'Hash Pipe',
   'Undone - The Sweater Song',
   'My Name Is Jonas',
   'Take On Me'],
  ['Truth Hurts',
   'Good As Hell',
   'Good As Hell (feat. Ariana Grande) - Remix',
   'Juice',
   'Boys',
   'Tempo (feat. Missy Elliott)',
   'Blame It on Your Love (feat. Lizzo)',
   'Soulmate',
   'Water Me',
   'Like A Girl']),
 'Release date': (['2013-04-05',
   '2013-04-05',
   '2017-05-12',
   '2007-06-11',
   '2009-09-28',
   '2009-09-28',
   '2017-05-12',
   '2017-05-12',
   '2007-06-11',
   '2009-09-28'],
  ['2001-05-15',
   '1994-05-10',
   '1994-05-10',
   '2005-05-10',
   '2019-01-24',
   '2019-09-10',
   '2001-05-15',
   '1994-05-10',
   '1994-05-10',
   '2019-01-24'],
  ['2019-05-03',
   '2016-03-09',
   '2019-10-25',
   '2019-04-19',
   '2019-04-18',
   '2019-04-19',
   '2019-09-13',
   '2019-04-19',
   '2019-04-18',
   '2019-04-19']),
 'Popularity score': ([76, 74, 73, 73, 72, 69, 66, 66, 65, 65],
  [77, 75, 73, 71, 67, 67, 66, 65, 63, 62],
  [94, 90, 86, 84, 72, 78, 68, 72, 58, 71])}

frame = collapse_nested_artist(d)

如果鍵值對中的所有值都具有相同的大小,則字典作為數據幀更容易處理,並且可以使其更直接。 如果可能的話,我會稍微重新格式化你的字典。 例如,將每一列嵌套到藝術家中以避免對位置的假設:

ex = {'foo':{'title':[1,2],'letter':['a','b']},
      'bar':{'title':[3,4],'letter':['c','d']}, 
      'fob':{'title':[5,6],'letter':['e','f']},
     }

df = []
for key, value in ex.items():
    minidf = pd.DataFrame(value)
    minidf['label'] = key
    df.append(minidf)
pd.concat(df, ignore_index=True)

將返回

   title letter label
0      1      a   foo
1      2      b   foo
2      3      c   bar
3      4      d   bar
4      5      e   fob
5      6      f   fob

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM