有条件地制作数据框元素列表

时间:2016-09-21 18:43:34

标签: pandas

以下是我的数据框的子部分:

                   addresses        lats
0                     street         lat
1                316 36th St         NaN
2             1400 Broadway          NaN
3                230 5th Ave         NaN
4                630 1st Ave     40.7452
5                 18 33rd St    40.74814
6                630 1st Ave     40.7452
7              1245 Park Ave    40.78659
8                444 46th St    40.76161
9                132 36th St    40.75145
10               146 29th St    40.74728
11               255 36th St    40.75326
12  1100 Ave of the Americas  40.7550567
13       777 Westchester Ave    41.02749
14                 9 57th St    40.76386
15               880 3rd Ave      40.758

我想列出addresses列中nan列中对应lats的所有元素的列表。

给定数据框的列表为:['316 13th st, '1400 broadway', '230 5th ave']

目前我正在使用以下代码,但它正在创建addresses中所有元素的列表。如前所述,我只想在下一列中包含nan的元素。这样做的有效方法是什么?

# creating my data frame from the columns
df = pd.read_csv(filename, header = None, error_bad_lines = False, usecols = [9, 28], names =
['addresses', 'lats'])
# converting the addresses column to a list
addresses = df['addresses'].tolist()
# removing any 'nan' from the addresses list (if any exist)
addresses = [x for x in addresses[1:] if str(x) != 'nan']

1 个答案:

答案 0 :(得分:1)

如何获取此列表。

df.loc[df['lats'].isnull(), 'addresses'].tolist()

['316 36th St', '1400 Broadway', '230 5th Ave']

注意:由于仅选择了列addressesloc选择器返回Series,这就是为什么可以使用tolist方法。