大熊猫[]中没有一个在索引中[重复]

扎眼的阳光 python 230

原文标题pandas None of [] are in the index [duplicate]

如果维度匹配,可以将列表或 numpy 数组解压缩为多个变量。对于 3xN 阵列,以下将起作用:

import numpy as np 
a,b =          [[1,2,3],[4,5,6]]
a,b = np.array([[1,2,3],[4,5,6]])
# result: a=[1,2,3],   b=[4,5,6]

如何为 pandasDataFrame 的列实现类似的行为?扩展上面的例子:

import pandas as pd 
df = pd.DataFrame([[1,2,3],[4,5,6]])
df.columns = ['A','B','C']    # Rename cols and
df.index = ['i', 'ii']        # rows for clarity

以下不按预期工作:

a,b = df.T
# result: a='i',   b='ii'
a,b,c = df
# result: a='A',   b='B',   c='C'

但是,我想得到的是以下内容:

a,b,c = unpack(df)
result: a=df['A'], b=df['B'], c=df['C']

功能unpack已经在 pandas 中可用了吗?或者它可以以简单的方式被模仿吗?

原文链接:https://stackoverflow.com//questions/71599557/pandas-none-of-are-in-the-index

回复

我来回复
  • normanius的头像
    normanius 评论

    我只是认为以下工作已经接近我试图实现的目标:

    a,b,c = df.T.values 
    # pd.DataFrame.as_matrix() is deprecated
    
    2年前 0条评论