2016-09-21 45 views
0

這裏是我的數據幀的小節的樣子:使列表有條件

    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

你試過了:'addresses = df [df.lats.isnull()]。addresses.tolist()'? –

+0

@JonClements一旦我在線後刪除'[1:]'',就行了。我最初是從名單中刪除「街道」。用你的線,它會刪除'street',因爲它旁邊沒有空值是正確的?所以我很安全地刪除我沒有正確的索引? – Harrison

回答

1

此處如何獲取此列表。

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

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

注意:由於只有在列中選擇addresses,所述loc選擇器返回一個Series,這就是爲什麼有可能使用tolist方法。