79

これは機能します(Pandas 12 devを使用)

table2=table[table['SUBDIVISION'] =='INVERNESS']

それから、「で始まる」を使用してフィールドを選択する必要があることに気付きました。だから、私が従うことができる限り近いパンダのドキュメントに従って、私は試しました

criteria = table['SUBDIVISION'].map(lambda x: x.startswith('INVERNESS'))
table2 = table[criteria]

そして AttributeError: 'float' object has no attribute 'startswith' を取得しました

だから私は同じ結果で別の構文を試しました

table[[x.startswith('INVERNESS') for x in table['SUBDIVISION']]]

参照http://pandas.pydata.org/pandas-docs/stable/indexing.html#boolean-indexing セクション 4: リスト内包表記と Series の map メソッドを使用して、より複雑な基準を作成することもできます。

私は何が欠けていますか?

4

4 に答える 4

104

str.startswithDataFrame メソッドを使用して、より一貫した結果を得ることができます。

In [11]: s = pd.Series(['a', 'ab', 'c', 11, np.nan])

In [12]: s
Out[12]:
0      a
1     ab
2      c
3     11
4    NaN
dtype: object

In [13]: s.str.startswith('a', na=False)
Out[13]:
0     True
1     True
2    False
3    False
4    False
dtype: bool

ブール値のインデックス作成は問題なく機能します (私は を使用することを好みますlocが、なくても同じように機能します)。

In [14]: s.loc[s.str.startswith('a', na=False)]
Out[14]:
0     a
1    ab
dtype: object

.

Series/column の要素の少なくとも 1 つが float であるように見えます。これには startswith メソッドがないため、AttributeError が発生し、リスト内包表記で同じエラーが発生するはずです...

于 2013-07-30T22:16:05.797 に答える
25

必要な文字列で始まるすべての行を取得するには

dataFrameOut = dataFrame[dataFrame['column name'].str.match('string')]

必要な文字列を含むすべての行を取得するには

dataFrameOut = dataFrame[dataFrame['column name'].str.contains('string')]
于 2018-03-25T16:31:34.860 に答える