0
我正在嘗試搜索熊貓數據框中的值。在熊貓中搜索字符串值
這是我的DF的樣子:
0 1 2 \
0 NaN NaN NaN
1 CITI Pass-T... NaN NaN
2 NaN NaN NaN
3 Certificateholder Distribution Summary NaN NaN
4 Class CUSIP Record Date
5 A-1 25151EAA1 12/30/2016
6 A-2 25151EAB9 12/30/2016
7 A-3 25151EAC7 12/30/2016
8 A-4 25151EAD5 12/30/2016
9 A-5A 25151EAE3 12/30/2016
10 A-5B 25151EAF0 12/30/2016
11 A-6 25151EAG8 12/30/2016
12 A-7 25151EAH6 12/30/2016
13 A-8 25151EAJ2 01/24/2017
14 M-1 25151EAK9 12/30/2016
15 M-2 25151EAL7 12/30/2016
16 M-3 25151EAM5 12/30/2016
17 M-4 25151EAN3 12/30/2016
18 M-5 25151EAP8 12/30/2016
19 M-6 25151EAQ6 12/30/2016
20 M-7 25151EAR4 12/30/2016
21 M-8 25151EAS2 12/30/2016
22 M-9 25151EAT0 12/30/2016
23 M-10 25151EAU7 12/30/2016
24 M-11 25151EAV5 12/30/2016
25 P 25151EAX1 12/30/2016
26 CE 25151EAW3 12/30/2016
27 R 25151EAY9 12/30/2016
28 Totals NaN NaN
29 This report is compiled by me, N... NaN NaN
30 All Record Dates are based upon the governing ... NaN NaN
31 NaN NaN NaN
所以你看,有沒有真正的列標題。 現在我想要例如搜索值A-1。
這是我做過什麼:
for col in df:
print col
print df[df[col].str.contains("A-1", na=False)]
這實際上給了我我想要的結果:
0 1 2 3 4 5 6 7 \
5 A-1 25151EAA1 12/30/2016 6.25 7218381.58 25379.0 143237.93 71982.98
8 9 10 11 12 13 14
5 7003160.66 168616.93 6169381.87 NaN NaN NaN NaN
但後來我得到以下錯誤:
AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas
是否有人有任何想法我做錯了什麼?
http://stackoverflow.com/questions/33692532/pandas-error-can-only-use -str-accessor-with-string-values這表明你的數據框中有一個空的列。在你的循環中,你可以嘗試編寫''col in df.select_dtypes([np.object])。columns [1:]:'而不是'col for df',看看它是否有效? – Chuck
您只能看看'df [df [0] .str.contains(「A-1」,na = False)]' – zipa
@ Charles。我有你的想法去工作。這是因爲列中的NaN值。我只需要將列[1:]:更改爲列[0:]:並且工作。 – Max