首页 > 解决方案 > pandas.dataframe.from_records 不适用于 numpy 数组

问题描述

让我们创建一个 numpy 数组和一个列表

import pandas as pd
import numpy as np

m = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
l = ["a", "b", "c"]

这有效:

> pd.DataFrame.from_records(m, index=l)

   0  1  2
a  1  2  3
b  4  5  6
c  7  8  9

这有效:

> pd.DataFrame.from_records(m, columns=l)
   a  b  c
0  1  2  3
1  4  5  6
2  7  8  9

但这不会:

pd.DataFrame.from_records(m, index=l, columns=l)
Empty DataFrame
Columns: []
Index: [(1, 2, 3), (4, 5, 6), (7, 8, 9)]

from_records如果我们不调用方法,这个小错误不会发生。但我需要它来避免 Dataframe 复制数据。

标签: pythonpandasnumpydataframe

解决方案


推荐阅读