从pandas.core.series.Series中提取数据时的KeyError

时间:2017-08-29 14:50:33

标签: python pandas

在下面的ipython3会话中,我读取了不同格式的表,并在其中一列中找到了值的总和:

In [278]: F = pd.read_table("../RNA_Seq_analyses/mapping_worm_number_tests/hisat2/mapped_C_elegans/feature_count/W100_1_on_C_elegans/protein_coding_fwd_counts.txt", skip
     ...: rows=2, usecols=[6]).sum()

In [279]: S = pd.read_table("../RNA_Seq_analyses/mapping_worm_number_tests/hisat2/mapped_C_elegans/intersect_count/W100_1_on_C_elegans/protein_coding_fwd_counts.txt", us
     ...: ecols=[6], header=None).sum()

In [280]: S
Out[280]: 
6    3551266
dtype: int64

In [281]: F
Out[281]: 
72    3164181
dtype: int64

In [282]: type(F)
Out[282]: pandas.core.series.Series

In [283]: type(S)
Out[283]: pandas.core.series.Series

In [284]: F[0]
Out[284]: 3164181

In [285]: S[0]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-285-5a4339994a41> in <module>()
----> 1 S[0]

/home/bli/.local/lib/python3.6/site-packages/pandas/core/series.py in __getitem__(self, key)
    601             result = self.index.get_value(self, key)
    602 
--> 603             if not is_scalar(result):
    604                 if is_list_like(result) and not isinstance(result, Series):
    605 

/home/bli/.local/lib/python3.6/site-packages/pandas/indexes/base.py in get_value(self, series, key)

pandas/index.pyx in pandas.index.IndexEngine.get_value (pandas/index.c:3323)()

pandas/index.pyx in pandas.index.IndexEngine.get_value (pandas/index.c:3026)()

pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4009)()

pandas/src/hashtable_class_helper.pxi in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:8146)()

pandas/src/hashtable_class_helper.pxi in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:8090)()

KeyError: 0

FS对象如果来自类似操作(sum)并且属于同一类型(pandas.core.series.Series),会有不同的行为吗

提取我想要的值(列的总和)的正确方法是什么?

编辑:尝试解决方案:

In [297]: F["72"]
Out[297]: 3164181

In [298]: S["6"]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4009)()

pandas/src/hashtable_class_helper.pxi in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:8125)()

TypeError: an integer is required

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-298-0127424036a0> in <module>()
----> 1 S["6"]

/home/bli/.local/lib/python3.6/site-packages/pandas/core/series.py in __getitem__(self, key)
    601             result = self.index.get_value(self, key)
    602 
--> 603             if not is_scalar(result):
    604                 if is_list_like(result) and not isinstance(result, Series):
    605 

/home/bli/.local/lib/python3.6/site-packages/pandas/indexes/base.py in get_value(self, series, key)

pandas/index.pyx in pandas.index.IndexEngine.get_value (pandas/index.c:3323)()

pandas/index.pyx in pandas.index.IndexEngine.get_value (pandas/index.c:3026)()

pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4075)()

KeyError: '6'

进一步调查:

In [306]: print(S.index)
Int64Index([6], dtype='int64')

In [307]: print(F.index)
Index(['72'], dtype='object')

In [308]: S[6]
Out[308]: 3551266

所以这两个对象最终有不同类型的索引。这种行为让我想起了R ......

似乎header=None导致列数为S的数字索引,而缺少header=Noneskiprows=2导致索引是根据读取的数据生成的第三排。 (这揭示了我在pandas中解析数据的方式中的一个错误......)

2 个答案:

答案 0 :(得分:3)

我认为你需要:

source_folder= DriveApp.getFolderById('YourFolderID1')
  dest_folder=DriveApp.getFolderById('YourFolderID2')

  var files = source_folder.getFilesByName("YourFileName").next();
  dest_folder.addFile(files);
  source_folder.removeFile(files);

或者:

#select first value of one element series
f = F.iat[0]
#alternative 
#f = F.iloc[0]

或者:

#convert to numpy array and select first value
f = F.values[0]

我认为你得到错误,因为没有索引值f = F.item()

正如IanS所评论的应该按索引值06选择:

72

样品:

f = F[72] 
#f = f.loc[72]

s = S[6]
#s = S.loc[6]
  

KeyError:0

您在F = pd.Series([3164181], index=[72]) f = F[72] print (f) 3164181 print (F.index) Int64Index([72], dtype='int64') print (F.index.tolist()) [72] f = F[0] print (f) 中获得一个整数索引,因为参数S - pandas添加默认索引(header=None)。对于0,1,...使用名为F的{​​{1}}列 - 它是字符串。有区别。

答案 1 :(得分:0)

我认为问题是您需要定义每个文件的分隔符。否则,read_table将读取一行作为列。

尝试以下方法: pd.read_table(filename,sep =&#34;;&#34;,usecols = [6])。sum()

相关问题