python - 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 复制数据。
解决方案
推荐阅读
- string - MongoDB字符串到int
- html - 如何将段落显示为水平列表?
- sql - 如何修复 Oracle11g 中的“ORA-01735: Invalid ALTER TABLE option”错误
- node.js - 从 Node 查询 MariaDB 时结果中的字符编码不一致
- python - 在 Python 中调用要返回的对象的方法时应该使用什么返回语法?
- ruby-on-rails - 基本 Rails 搜索不呈现结果
- azure-cognitive-search - Azure 搜索和必应搜索有什么区别以及何时使用?
- google-compute-engine - 如何仅允许某些 google-cloud-functions 访问
- html - 如何创建代码块,这是一种在我的网站上显示我的代码的好方法,因此人们也可以复制代码
- tinybutstrong - 如何在条件为空值的情况下获得 tinybutstrong 合并块