MYO TextMiningToolkit
MYO TextMiningToolkit

Reputation: 123

how to check in a pandas DataFrame entry exists by (index, column)

I have searched and searched but I can't find how to test whether a pandas data frame entry exists by (index, column).

for example:

import pandas
df = pandas.DataFrame()

df.ix['apple', 'banana'] = 0.1
df.ix['apple', 'orange'] = 0.1
df.ix['kiwi', 'banana'] = 0.2
df.ix['kiwi', 'orange'] = 0.7

df
       banana  orange
apple     0.1     0.1
kiwi      0.2     0.7

Now I want to test whether an entry exists

if (["apple", "banana"] in df)

.. should be True

and

if (["apple", "apple"] in df)

.. should be False


Actually the reason I'm testing for existence is because the following doesn't work

some loop ..
    df[wp] += some_value
    pass

It fails when df[wp] doesn't exist. This does work if the expression is

some loop ..
    df[wp] += some_value
    pass

because pandas does have the idea of extending an array through assignment.

Upvotes: 1

Views: 3361

Answers (1)

user2285236
user2285236

Reputation:

You can check for the existence in index and columns:

('kiwi' in df.index) and ('apple' in df.columns)

Or you can use a try/except block:

try:
    df.ix['kiwi', 'apple'] += some_value
except KeyError:
    df.ix['kiwi', 'apple'] = some_value

Note that DataFrames' shapes are not meant to be dynamic. This may slow down your operations heavily. So it might be better to do these things with dictionaries and finally turn them into DataFrames.

Upvotes: 1

Related Questions