以下是我的数据框的子部分:
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']
答案 0 :(得分:1)
如何获取此列表。
df.loc[df['lats'].isnull(), 'addresses'].tolist()
['316 36th St', '1400 Broadway', '230 5th Ave']
注意:由于仅选择了列addresses
,loc
选择器返回Series
,这就是为什么可以使用tolist
方法。