使用非唯一索引对象创建面板

2024-06-25 23:31:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下数据文件LawSchoolSample.csv

LSAT,GPA
622,3.23
542,2.83
579,3.24
653,3.12
606,3.09

我想创建一个pandas数据帧,然后从这个数据帧重新采样B次以形成一个pandas面板。以下是我的尝试(欢迎评论):

import pandas as pd

df = pd.read_csv("LawSchoolSample.csv")

B = 3
resamples = {}

for i in range(0,B):
    name = "Resample {}".format(i)
    resamples[name] = df.sample(5,replace=True)

print resamples

resamples_panel = pd.Panel(resamples)

除了最后一行:resamples_panel = pd.Panel(resamples)一切正常。错误是:

pandas.core.index.InvalidIndexError: Reindexing only valid with uniquely valued Index objects

我有两个问题:

  1. 使用panel值得吗?或者是任何数据结构resamples都足够好吗?你知道吗
  2. 向面板添加dataframes的首选方法是什么?你知道吗

Tags: csv数据name面板pandasdf数据文件评论
1条回答
网友
1楼 · 发布于 2024-06-25 23:31:24

长期计划是反对Panel,见熊猫文档:

In a future version of pandas, we will be deprecating Panel and other >2 ndim objects. In order to provide for continuity, all NDFrame objects have gained the .to_xarray() method in order to convert to xarray objects, which has a pandas-like interface for > 2 ndim. (GH11972)

http://pandas.pydata.org/pandas-docs/version/0.18.0/whatsnew.html#to-xarray

相关问题 更多 >