Take long list of items and reshape into dataframe “rows” - pandas python 3

故事扮演 提交于 2021-02-05 06:35:06

问题


I have a long list of items that I want to put in a data frame at set intervals. I have another list with "column names".

E.g.

colnames = ['Title', 'Date', 'Abstract', 'ID', 'Volume']
data = [a, b, c, d, e, f, g, h, i ,j, k, l, m, n, o]

I want to create a data frame that looks like:

    |   Title   |   Date   |   Abstract   |   ID   |   Volume   
__________________________________________________________________

0         a          b            c           d          e
1         f          g            h           i          j
2         k          l            m           n          o

Thanks for any suggestions!


回答1:


You need DataFrame constructor with numpy.reshape:

import pandas as pd
import numpy as np

colnames = ['Title', 'Date', 'Abstract', 'ID', 'Volume']
data = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' ,'j', 'k', 'l', 'm', 'n', 'o']

df = pd.DataFrame(np.array(data).reshape(-1, len(colnames)), columns=colnames)
print (df)
  Title Date Abstract ID Volume
0     a    b        c  d      e
1     f    g        h  i      j
2     k    l        m  n      o

But if some values are missing is possible use unstack:

colnames = ['Title', 'Date', 'Abstract', 'ID', 'Volume']
data = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' ,'j', 'k', 'l', 'm']

df = pd.DataFrame(data, columns=['col'])
df.index = [df.index // len(colnames), df.index % len(colnames)]
df = df['col'].unstack()
df.columns = colnames
print (df)
  Title Date Abstract    ID Volume
0     a    b        c     d      e
1     f    g        h     i      j
2     k    l        m  None   None


来源:https://stackoverflow.com/questions/42719765/take-long-list-of-items-and-reshape-into-dataframe-rows-pandas-python-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!