1

Could you tell me how to group a table (from products1.txt file) like following:

Age;Name;Country
10;Valentyn;Ukraine
12;Igor;Russia
12;Valentyn;
10;Valentyn;Russia

So I can find out how many Valentyns have an empty "Country" cell.
I ran the following code:

import pandas as pd
df = pd.read_csv('d:\products1.txt', sep = ";")
result = df[(df["Name"] == "Valentyn") & (df["Country"] == None)]

But I get an error...

1 Answer 1

2

You should use isnull (rather than == None) to check for NaN:

In [11]: df[(df.Country.isnull()) & (df.Name == 'Valentyn')]
Out[11]:
   Age      Name Country
2   12  Valentyn     NaN

Another option would be to check those which had Country NaN and then count the values:

In [12]: df.Name[df.Country.isnull()]
Out[12]:
2    Valentyn
Name: Name, dtype: object

In [13]: df.Name[df.Country.isnull()].value_counts()
Out[13]:
Valentyn    1
dtype: int64
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.