【问题标题】:Value Error Obtained while creating a new dataframe which contains the min and max of a particular column创建包含特定列的最小值和最大值的新数据框时获得的值错误
【发布时间】:2021-06-04 14:22:05
【问题描述】:

我有一个如下所示的数据框。我需要找到“Chip_Current[uAmp]”列的最小值和最大值我使用下面的代码创建了一个新的数据框,但获得了一个错误。

ValueError:传递了 2 列,传递的数据有 1 列。

data = [[DEEP_SLEEP_PVT['Chip_Current[uAmp]'].min()], [DEEP_SLEEP_PVT['Chip_Current[uAmp]'].max()]]
df_DS = pd.DataFrame(data, columns = ['MIN', 'MAX'])

DEEP_SLEEP_PVT 是我的数据透视表名称

运行 Jerzel 提供的代码后,输出如期而至。请看下文。

【问题讨论】:

  • 该错误告诉您正在尝试连接 RHS 中的两列,就好像它们是一个普通的旧 Python 列表一样:data = [[DS['CC'].min()], [DS['CC'].max()]。但它们是系列,所以你应该使用pd.concat([DS['CC'].min()], [DS['CC'].max()])。或者 jezrael 建议的替代方案。
  • 如果你重命名你的数据框ds 和你的列'Chip_Current[uAmp]' -> 'CC',它有助于你的代码清晰。在 Python 中,像“DEEP_SLEEP_PVT”这样的变量名应该总是小写(除非它们是常量)。列名可以是任何你想要的,但简短的代码会更好。

标签: python pandas dataframe


【解决方案1】:

使用Series.aggregate,输出为Series,因此将一行DataFrame转换为DataFrame并转置:

df1 = DEEP_SLEEP_PVT['Chip_Current[uAmp]'].agg(['min','max']).to_frame().T

如果需要像Device_ID 这样的其他列/级别的最小值和最大值,请使用GroupBy.agg

df2 = DEEP_SLEEP_PVT.groupby('Device_ID')['Chip_Current[uAmp]'].agg(['min','max'])

或按多个列/级别:

df3 = (DEEP_SLEEP_PVT.groupby(['Device_ID', 'Temp(deg)'])['Chip_Current[uAmp]']
                     .agg(['min','max']))

样本数据:

DEEP_SLEEP_PVT = (pd.DataFrame({'Chip_Current[uAmp]':[5,0,-1,2,3,2,1,-5,7,4.8],
                               'Device_ID':['FF_2646'] * 5 + ['TT_2438'] * 5,
                               'Temp(deg)':[-20]*3 + [25] * 2 + [-20]*3 + [25] * 2})
                    .set_index(['Device_ID','Temp(deg)']))
    
print (DEEP_SLEEP_PVT)
                     Chip_Current[uAmp]
Device_ID Temp(deg)                    
FF_2646   -20                       5.0
          -20                       0.0
          -20                      -1.0
           25                       2.0
           25                       3.0
TT_2438   -20                       2.0
          -20                       1.0
          -20                      -5.0
           25                       7.0
           25                       4.8

df1 = DEEP_SLEEP_PVT['Chip_Current[uAmp]'].agg(['min','max']).to_frame().T
print (df1)
                    min  max
Chip_Current[uAmp] -5.0  7.0

df2 = DEEP_SLEEP_PVT.groupby('Device_ID')['Chip_Current[uAmp]'].agg(['min','max'])
print (df2)
           min  max
Device_ID          
FF_2646   -1.0  5.0
TT_2438   -5.0  7.0

df3 = DEEP_SLEEP_PVT.groupby(['Device_ID', 'Temp(deg)'])['Chip_Current[uAmp]'].agg(['min','max'])
print (df3)
                     min  max
Device_ID Temp(deg)          
FF_2646   -20       -1.0  5.0
           25        2.0  3.0
TT_2438   -20       -5.0  2.0
           25        4.8  7.0

【讨论】:

  • 它正在工作。我更新了我的问题。我需要在 max 列之后使用 spec max 列。
  • @HARITO - 不确定是否理解,需要DEEP_SLEEP_PVT.agg(['min','max']).T 吗?
  • 非常有帮助。谢谢
  • "to_frame().T" .我可以知道“.T”在这里做什么
  • @HARITO - 转置DataFrame.T
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-09
  • 1970-01-01
  • 2012-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-09
相关资源
最近更新 更多