简体   繁体   中英

Pandas pivot table cufflinks plot

I have a pivot table named "df_thd_funct_mode1_PVT".It looks like as shown below.

在此处输入图像描述

Below is my code for plotting the pivot chart.

THD_PVT_2V5 = df_thd_funct_mode1_PVT[df_thd_funct_mode1_PVT['Supply'] == 2.5].pivot_table(index='Temp', columns='xvalues', values=['94','100','110','115','120','124','128','129','130'])
# THD_PVT_2V5['SPEC_MIN']= 37
# THD_PVT_2V5['SPEC_TYP']= 38.3
# THD_PVT_2V5['SPEC_MAX']= 39.6
THD_PVT_2V5.iplot(title='THD vs TEMPERATURE @ 2.5V FUNCTIONAL RANGE', xaxis_title='TEMPERATURE', yaxis_title='THD',width=3)

As per my knowledge all my key values are correct,but I am getting an error.It is given below.

"KeyError                                  Traceback (most recent call last)
File C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py:3621, in Index.get_loc(self, key, method, tolerance)
   3620 try:
-> 3621     return self._engine.get_loc(casted_key)
   3622 except KeyError as err:

File C:\ProgramData\Anaconda3\lib\site-packages\pandas\_libs\index.pyx:136, in pandas._libs.index.IndexEngine.get_loc()

File C:\ProgramData\Anaconda3\lib\site-packages\pandas\_libs\index.pyx:163, in pandas._libs.index.IndexEngine.get_loc()

File pandas\_libs\hashtable_class_helper.pxi:5198, in pandas._libs.hashtable.PyObjectHashTable.get_item()

File pandas\_libs\hashtable_class_helper.pxi:5206, in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'Supply'

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
Input In [249], in <cell line: 1>()
----> 1 THD_PVT_2V5 = df_thd_funct_mode1_PVT[df_thd_funct_mode1_PVT['Supply'] == 2.5].pivot_table(index='Temp', columns='xvalues', values=['94','100','110','115','120','124','128','129','130'])
      2 # THD_PVT_2V5['SPEC_MIN']= 37
      3 # THD_PVT_2V5['SPEC_TYP']= 38.3
      4 # THD_PVT_2V5['SPEC_MAX']= 39.6
      5 THD_PVT_2V5.iplot(title='THD vs TEMPERATURE @ 2.5V FUNCTIONAL RANGE', xaxis_title='TEMPERATURE', yaxis_title='THD',width=3)

File C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py:3505, in DataFrame.__getitem__(self, key)
   3503 if self.columns.nlevels > 1:
   3504     return self._getitem_multilevel(key)
-> 3505 indexer = self.columns.get_loc(key)
   3506 if is_integer(indexer):
   3507     indexer = [indexer]

File C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py:3623, in Index.get_loc(self, key, method, tolerance)
   3621     return self._engine.get_loc(casted_key)
   3622 except KeyError as err:
-> 3623     raise KeyError(key) from err
   3624 except TypeError:
   3625     # If we have a listlike key, _check_indexing_error will raise
   3626     #  InvalidIndexError. Otherwise we fall through and re-raise
   3627     #  the TypeError.
   3628     self._check_indexing_error(key)

KeyError: 'Supply'
"

Sample pivot table is given below

        94  100 110 115 120 124 128 129 130
Temp    xvalues Supply                                  
-40 MAIN_001    2.5 0.1644  0.0844  0.032   0.1202  0.3129  0.2177  2.9942  6.9715  9.9139
        2.7 0.1591  0.08545 0.03065 0.12305 0.32265 0.22645 2.57495 6.54425 9.49495
        3.6 0.15775 0.083   0.02825 0.112   0.32735 0.2325  2.7538  6.72915 9.57755
-60 MAIN_005    2.5 0.1868  0.0972  0.0521  0.6005  0.8448  0.7479  5.157   45.9854 0.7479
        2.7 0.2047  0.1068  0.0532  0.603   0.8502  0.7521  5.1481  39.4838 0.7521
        3.6 0.1909  0.0992  0.0567  0.5917  0.8612  0.7626  5.1195  42.9942 0.7626

May I know where I went wrong.

arrays = [np.array([-40, -40, -40, -60, -60, -60]), np.array(["MAIN_001", "MAIN_001", "MAIN_001", "MAIN_005", "MAIN_005", "MAIN_005",]), np.array([2.5, 2.7, 3.6, 2.5, 2.7, 3.6])]
df = pd.DataFrame({94: [0.1644, 0.1591, 0.15775, 0.1868, 0.2047, 0.1909],
                   100: [0.0844, 0.08545, 0.083, 0.0972, 0.1068, 0.0992],
                   110: [0.032, 0.03065, 0.02825, 0.0521, 0.0532, 0.0567],
                   115: [0.1202, 0.12305, 0.112, 0.6005, 0.603, 0.5917],
                   120: [0.3129, 0.32265, 0.32735, 0.8448, 0.8502, 0.8612],
                   124: [0.2177, 0.22645, 0.2325, 0.7479, 0.7521, 0.7626],
                   128: [2.9942, 6.9715, 9.9139, 5.157, 39.4838, 42.9942],
                   129: [6.9715, 9.9139, 0.7479, 45.9854, 0.7479, 0.7521],
                   130: [9.9139, 0.7479, 0.7521, 0.7479, 0.7521, 0.7626]}, index=pd.MultiIndex.from_arrays(arrays, names=['Temp', 'Supply', 'xvalues']))
df

在此处输入图像描述




The problem is the dataframe with MultiIndex , we select it with get_level_values()

df_select = df.iloc[df.index.get_level_values('xvalues') == 2.5]
df_select.iplot(title='THD vs TEMPERATURE @ 2.5V FUNCTIONAL RANGE', xaxis_title='TEMPERATURE', yaxis_title='THD',width=3)

在此处输入图像描述 Please note that I disorder the sequence of index name Temp , Supply , xvalues . in yours, it should be Temp , xvalues , Supply . Therefore the selection would be df_select = df.iloc[df.index.get_level_values('Supply') == 2.5]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM