2016-05-06 79 views
1

我使用impyla从Impala提取数据,并使用as_pandas将它们转换为数据框。我正在使用Pandas 0.18.0Python 2.7.9不计算大熊猫数据框中所有列的总和

我想计算一个数据框中所有列的总和,并试图选择大于阈值的列。

self.data = self.data.loc[:,self.data.sum(axis=0) > 15]

但是当我运行此我得到错误象下面这样:

pandas.core.indexing.IndexingError: Unalignable boolean Series key provided

然后我试图像下面。

print 'length : ',len(self.data.sum(axis = 0)),' all columns : ',len(self.data.columns)

然后我得到不同的长度即

length : 78 all columns : 83

而且我得到以下警告

C:\Python27\lib\decimal.py:1150: RuntimeWarning: tp_compare didn't return -1 or -2 for exception

,实现我的目标我试过其他方式

for column in self.data.columns: 
    sum = self.data[column].sum() 
    if(sum < 15): 
     self.data = self.data.drop(column,1) 

现在我已经得到了像下面的其他错误:

TypeError: unsupported operand type(s) for +: 'Decimal' and 'float' C:\Python27\lib\decimal.py:1150: RuntimeWarning: tp_compare didn't return -1 or -2 for exception

然后我试图让每列的数据类型,如下面。

print 'dtypes : ', self.data.dtypes 

结果具有所有列均这些的int64,对象的一个​​和浮64 然后,我想改变其在对象像下面

self.data.convert_objects(convert_numeric=True) 

仍然列的数据类型的我得到了同样的错误,请帮助我解决这个问题。

注:在所有我没有字符串,即字符和缺失值或empty.I已经检查了这列使用self.data.to_csv

由于我是新来的熊猫和蟒蛇请不要介意这是一个愚蠢的问题。我只是想学习

回答

0

请检查下面的简单代码,你可能会明白错误的原因。

import pandas as pd 
import numpy as np 


df = pd.DataFrame(np.random.random([3,3])) 
df.iloc[0,0] = np.nan 

print df 
print df.sum(axis=0) > 1.5 
print df.loc[:, df.sum(axis=0) > 1.5] 

df.iloc[0,0] = 'string' 

print df 
print df.sum(axis=0) > 1.5 
print df.loc[:, df.sum(axis=0) > 1.5] 

      0   1   2 
0  NaN 0.336250 0.801349 
1 0.930947 0.803907 0.139484 
2 0.826946 0.229269 0.367627 

0  True 
1 False 
2 False 
dtype: bool 

      0 
0  NaN 
1 0.930947 
2 0.826946 

      0   1   2 
0 string 0.336250 0.801349 
1 0.930947 0.803907 0.139484 
2 0.826946 0.229269 0.367627 

1 False 
2 False 
dtype: bool 

Traceback (most recent call last): 
... 
pandas.core.indexing.IndexingError: Unalignable boolean Series key provided 

不久,您需要对您的数据进行额外的预处理。

df.select_dtypes(include=['object']) 

如果它是可转换串号,您可以通过df.astype()转换,或者你应该清除它们。

+0

在所有的列中,我只有数字既没有字符串也没有南。在这个问题上加了这个点 –

+0

@ManojKumar'pd.to_csv()'不保证你的数据框的值类型。它是后面的。 'self.data后检查了dtypes。convert_objects(convert_numeric = True)'再次?现在没有更多的“对象”类型?如果没有,也许你没有像'self.data = self.data.convert_objects(convert_numeric = True)'那样放置。请检查。 – su79eu7k

+0

它正在工作我失踪的任务谢谢@ su79eu7k –