Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 按多索引删除数据帧行_Python_Python 2.7_Pandas - Fatal编程技术网

Python 按多索引删除数据帧行

Python 按多索引删除数据帧行,python,python-2.7,pandas,Python,Python 2.7,Pandas,我想使用MultiIndex值从pandas数据帧中删除行 我试过很多东西,但我把我认为更接近的东西放在下面。(事实上,我将解释整个问题,因为可能有一种使用完全不同方法的替代解决方案)。从相关矩阵中,我想得到一对相关度更高的列。我使用unstack并将结果放入数据框中: In [263]: corr_df = pd.DataFrame(total.corr().unstack()) 然后得到更高的相关性(实际上我也应该得到负数) [264]中的high=corr_-df[(corr_-df[0

我想使用MultiIndex值从pandas数据帧中删除行

我试过很多东西,但我把我认为更接近的东西放在下面。(事实上,我将解释整个问题,因为可能有一种使用完全不同方法的替代解决方案)。从相关矩阵中,我想得到一对相关度更高的列。我使用
unstack
并将结果放入数据框中:

In [263]: corr_df = pd.DataFrame(total.corr().unstack())
然后得到更高的相关性(实际上我也应该得到负数)

[264]中的
high=corr_-df[(corr_-df[0]>0.5)和(corr_-df[0]<1.0)]
In[236]:打印高
0
残糖密度0.552517
游离二氧化硫总二氧化硫0.720934
总二氧化硫游离二氧化硫0.720934
葡萄酒0.700357
密度残糖0.552517
葡萄酒总二氧化硫0.700357
足够接近,但有重复,这实际上是相关矩阵的点。为了清理它们,我的想法是迭代高值以删除重复项:

In [267]:
for row in high.iterrows():
    print row[0][0], ",", row[0][1]
    print high.loc[row[0][1]].loc[row[0][0]].index
    high.drop(high.loc[row[0][1]].loc[row[0][0]].index)
residual sugar , density
Int64Index([0], dtype='int64')
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-267-1258da2a4772> in <module>()
      2     print row[0][0], ",", row[0][1]
      3     print high.loc[row[0][1]].loc[row[0][0]].index
----> 4     high.drop(high.loc[row[0][1]].loc[row[0][0]].index)

...
[huge stack of errors]
...
KeyError: 0
[267]中的

对于high.iterrows()中的行:
打印行[0][0],“,”,行[0][1]
打印high.loc[第[0][1]行].loc[第[0][0]行].index
high.drop(high.loc[row[0][1]].loc[row[0][0]].index)
残糖、密度
int64索引([0],dtype='int64')
---------------------------------------------------------------------------
KeyError回溯(最近一次呼叫最后一次)
在()
2打印第[0][0]、“、”、第[0][1]行
3打印高.loc[第[0][1]行].loc[第[0][0]行].index
---->4.高位下跌(高位loc[第[0][1]行].loc[第[0][0]行].index)
...
[大量错误]
...
关键错误:0
当索引正常时,
drop
方法可以很好地工作(请参阅),但是,当我得到一个
多索引时,如何构建
标签(
corr_df = pd.DataFrame(
{'residual sugar': [1, 0, 0, 0.552517, 0], 
'free sulfur dioxide': [0, 1, 0.720934, 0, 0], 
'total sulfur dioxide': [0, 0.720934, 1, 0, 0.700357],
'density': [0.552517, 0, 0, 1, 0],
'wine': [0, 0, 0.700357, 0, 1]}, 
index=['residual sugar', 'free sulfur dioxide', 'total sulfur dioxide', 'density', 'wine']).unstack()

# Notice the slight modification to the original
high = corr_df[(corr_df > 0.5) & (corr_df < 1.0)]

# Sort by index, then values
high.sort_index()
high.sort()

# Drop every other value (e.g. just take the evens)
result = high.iloc[[count for count, _ in enumerate(high) if count % 2 == 0]]
>>> result
density               residual sugar          0.552517
total sulfur dioxide  wine                    0.700357
free sulfur dioxide   total sulfur dioxide    0.720934
{‘残余糖’:[1,0,0,0.552517,0], “游离二氧化硫”:[0,1,0.720934,0,0], “总二氧化硫”:[0,0.720934,1,0,0.700357], “密度”:[0.552517,0,0,1,0], “葡萄酒”:[0,0,0.700357,0,1]}, 指数=[‘残余糖’、‘游离二氧化硫’、‘总二氧化硫’、‘密度’、‘葡萄酒’)。unstack() #请注意对原始文件的轻微修改 高=corr_-df[(corr_-df>0.5)和(corr_-df<1.0)] #按索引排序,然后按值排序 高。排序_索引() high.sort() #每隔一个值就删除一次(例如,只需使用evens) result=high.iloc[[计数的计数,如果计数%2==0,则在枚举中(高)] >>>结果 密度残糖0.552517 二氧化硫总量0.700357 游离二氧化硫总二氧化硫0.720934
corr_df = pd.DataFrame(
{'residual sugar': [1, 0, 0, 0.552517, 0], 
'free sulfur dioxide': [0, 1, 0.720934, 0, 0], 
'total sulfur dioxide': [0, 0.720934, 1, 0, 0.700357],
'density': [0.552517, 0, 0, 1, 0],
'wine': [0, 0, 0.700357, 0, 1]}, 
index=['residual sugar', 'free sulfur dioxide', 'total sulfur dioxide', 'density', 'wine']).unstack()

# Notice the slight modification to the original
high = corr_df[(corr_df > 0.5) & (corr_df < 1.0)]

# Sort by index, then values
high.sort_index()
high.sort()

# Drop every other value (e.g. just take the evens)
result = high.iloc[[count for count, _ in enumerate(high) if count % 2 == 0]]
>>> result
density               residual sugar          0.552517
total sulfur dioxide  wine                    0.700357
free sulfur dioxide   total sulfur dioxide    0.720934