Pandas:从系列创建数据帧 [英] Pandas: Creating DataFrame from Series
问题描述
我当前的代码如下所示 - 我正在导入一个 MAT 文件并尝试从其中的变量创建一个 DataFrame:
My current code is shown below - I'm importing a MAT file and trying to create a DataFrame from variables within it:
mat = loadmat(file_path) # load mat-file
Variables = mat.keys() # identify variable names
df = pd.DataFrame # Initialise DataFrame
for name in Variables:
B = mat[name]
s = pd.Series (B[:,1])
因此在循环中我可以创建一系列每个变量(它们是具有两列的数组 - 所以我需要的值在第 2 列中)
So within the loop I can create a series of each variable (they're arrays with two columns - so the values I need are in column 2)
我的问题是如何将系列附加到数据框?我已经浏览了文档,但似乎没有一个示例适合我正在尝试做的事情.
My question is how do I append the series to the dataframe? I've looked through the documentation and none of the examples seem to fit what I'm trying to do.
此致,
本
推荐答案
这里是如何创建一个DataFrame,其中每个系列都是一行.
Here is how to create a DataFrame where each series is a row.
对于单个系列(导致单行数据帧):
For a single Series (resulting in a single-row DataFrame):
series = pd.Series([1,2], index=['a','b'])
df = pd.DataFrame([series])
对于具有相同索引的多个系列:
For multiple series with identical indices:
cols = ['a','b']
list_of_series = [pd.Series([1,2],index=cols), pd.Series([3,4],index=cols)]
df = pd.DataFrame(list_of_series, columns=cols)
对于可能具有不同索引的多个系列:
For multiple series with possibly different indices:
list_of_series = [pd.Series([1,2],index=['a','b']), pd.Series([3,4],index=['a','c'])]
df = pd.concat(list_of_series, axis=1).transpose()
要创建一个每个系列都是一列的 DataFrame,请参阅其他人的答案.或者,可以创建一个 DataFrame,其中每个系列都是一行,如上所述,然后使用 df.transpose()
.但是,如果列具有不同的数据类型,则后一种方法效率低下.
To create a DataFrame where each series is a column, see the answers by others. Alternatively, one can create a DataFrame where each series is a row, as above, and then use df.transpose()
. However, the latter approach is inefficient if the columns have different data types.
这篇关于Pandas:从系列创建数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!