问题
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