【问题标题】:Unpack an indexed object of tuples解压元组的索引对象
【发布时间】:2021-12-24 07:50:09
【问题描述】:

我有一个从数据框的“groupby”函数派生的对象 (prec_score)。该对象有一个索引列(source)和一个 2 元素元组的未命名列。

我想将元组解压/解压成两个单独的列“mean”和“precision”。

这就是我所拥有的

prec_score = psi.groupby('source').apply(auc_group)

source
P03                     (0.0042910503473030034, 0.004975514927052615)
P04a                    (0.1722996231123154, 0.19441195500593225)
P04b                    (0.26313913996999294, 0.31874320468051276)
P04c                    (0.2612986898318525, 0.2541227790666698)
P04d                    (0.4529458009123246, 0.49560813661716663)
P04e                    (0.5467338804179422, 0.6847925038107519)
P06                     (0.003373416937568991, 0.007662479097491571)
P08                     (0.001412431535914154, 0.005282299334282409)
P09                     (0.011918926133313022, 0.00913808688008598)
P10                     (0.020750483361235646, 0.02767490357333552)
P11                     (0.06496517302643716, 0.049191221238955335)
P12                     (0.015587295242605693, 0.009555300604012188)
P13                     (0.04052937620271101, 0.03807649224213866)
P14                     (0.012883159363864815, 0.00843085863452591)
P15                     (0.006228328662793101, 0.006527283344563865)
dtype:object

这就是我想要的:

source                          mean                precision
P03                     0.0042910503473030034   0.004975514927052615
P04a                    0.1722996231123154      0.19441195500593225
P04b                    0.26313913996999294     0.31874320468051276
P04c                    0.2612986898318525      0.2541227790666698
P04d                    0.4529458009123246      0.49560813661716663
P04e                    0.5467338804179422      0.6847925038107519
P06                     0.003373416937568991    0.007662479097491571
P08                     0.001412431535914154    0.005282299334282409
P09                     0.011918926133313022    0.00913808688008598
P10                     0.020750483361235646    0.02767490357333552
P11                     0.06496517302643716     0.049191221238955335
P12                     0.015587295242605693    0.009555300604012188
P13                     0.04052937620271101     0.03807649224213866
P14                     0.012883159363864815    0.00843085863452591
P15                     0.006228328662793101    0.006527283344563865

【问题讨论】:

  • psi 是什么?
  • @balderman psi 是一个数据框
  • 熊猫 df ?用 pandas 标记问题是有意义的
  • @balderman,是的 pandas df,我会重新标记。谢谢。
  • @balderman,我没有包含 pandas 数据框的原因是,在我执行“groupby”之后,对象“prec_score”将自身呈现为“对象”类型。我执行了“Print(prec_score.info)”并收到“SERIES OBJECT HAS NO ATTRIBUTE 'TYPE' 消息

标签: python pandas tuples unzip


【解决方案1】:

编辑: iloc 使用不当。这行得通

df[['mean', 'precision']] = pd.DataFrame.from_records(df[1], index=df.index)

适用于这个df

df=pd.DataFrame(data=[['P03',(0.0042910503473030034,0.004975514927052615)], ['P04a',(0.1722996231123154,0.19441195500593225)])

【讨论】:

  • p_score[['mean','precision']] = pd.DataFrame(p_score.iloc[[1]].tolist(), index=p_score.index) Traceback(最近一次调用最后一次): 文件“C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\series.py”,第 972 行,在 setitem self._set_with_engine(key, value) 文件“C: \ProgramData\Anaconda3\lib\site-packages\pandas\core\series.py”,第 1005 行,在 _set_with_engine loc = self.index._engine.get_loc(key) 文件“pandas_libs\index.pyx”,第 70 行,在pandas._libs.index.IndexEngine.get_loc
  • File "pandas_libs\index.pyx", line 75, in pandas._libs.index.IndexEngine.get_loc TypeError: '['mean', 'precision']' is an invalid key 处理期间上面的异常,又发生了一个异常: Traceback (most recent call last): File "", line 1, in p_score[['mean','precision']] = pd .DataFrame(p_score.iloc[[1]].tolist(), index=p_score.index) 文件“C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\series.py”,第 998 行,在设置项
  • self._set_with(key, value) 文件“C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\series.py”,第 1035 行,在 _set_with self.loc[key ] = 值文件“C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexing.py”,第 688 行,在 setitem 中 indexer = self._get_setitem_indexer(key) 文件“C :\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexing.py",第 636 行,在 _get_setitem_indexer 返回 self._convert_to_indexer(key, axis=0, is_setter=True) 文件“C:\ProgramData\Anaconda3\ lib\site-packages\pandas\core\indexing.py",
  • 文件“C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexing.py”,第 1212 行,在 _convert_to_indexer 返回 self._get_listlike_indexer(key, axis, raise_missing=True) [1]
  • 不应该使用 iloc。它是固定的
猜你喜欢
  • 1970-01-01
  • 2016-04-10
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 2019-03-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多