您能告诉我如何对一个表(从products1.txt文件中)进行分组吗?
Age;Name;Country
10;Valentyn;Ukraine
12;Igor;Russia
12;Valentyn;
10;Valentyn;Russia这样我就能找出有多少情人节有一个空的“国家”单元。
我运行了以下代码:
import pandas as pd
df = pd.read_csv('d:\products1.txt', sep = ";")
result = df[(df["Name"] == "Valentyn") & (df["Country"] == None)]但我得到了一个错误...
发布于 2013-06-06 15:57:37
您应该使用isnull (而不是== None)来检查NaN
In [11]: df[(df.Country.isnull()) & (df.Name == 'Valentyn')]
Out[11]:
Age Name Country
2 12 Valentyn NaN另一种选择是检查那些具有国家/地区NaN的值,然后计算其值:
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: int64https://stackoverflow.com/questions/16956460
复制相似问题