Pandas 数据框中的不可哈希类型错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41796290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:50:35  来源:igfitidea点击:

Unhashable type error in pandas dataframe

pythonpandas

提问by user308827

I have the foll. pandas dataframe:

我有一个愚蠢的。Pandas数据框:

df.shape

(86, 245)

However, when I do this:

但是,当我这样做时:

df[0, :]

I get the error:

我收到错误:

*** TypeError: unhashable type

How do I fix this? I just want to get the first row

我该如何解决?我只想得到第一行

回答by jezrael

If need first row as Seriesjust use DataFrame.iloc:

如果需要第一行,Series只需使用DataFrame.iloc

df.iloc[0, :]

But if need DataFrameuse ilocbut add []or use head:

但如果需要DataFrame使用iloc但添加[]或使用head

df.iloc[[0], :]
df.head(1)

Sample:

样本:

df = pd.DataFrame({'A':[1,2,3],
                   'B':[4,5,6],
                   'C':[7,8,9],
                   'D':[1,3,5],
                   'E':[5,3,6],
                   'F':[7,4,3]})

print (df)
   A  B  C  D  E  F
0  1  4  7  1  5  7
1  2  5  8  3  3  4
2  3  6  9  5  6  3

print (df.iloc[0, :])
A    1
B    4
C    7
D    1
E    5
F    7
Name: 0, dtype: int64

print (df.head(1))
   A  B  C  D  E  F
0  1  4  7  1  5  7

print (df.iloc[[0], :])
   A  B  C  D  E  F
0  1  4  7  1  5  7