Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如果另一列的条目在列表中为nan,则将dataframe条目附加到列表中?_Python_Pandas_Lambda_List Comprehension - Fatal编程技术网

Python 如果另一列的条目在列表中为nan,则将dataframe条目附加到列表中?

Python 如果另一列的条目在列表中为nan,则将dataframe条目附加到列表中?,python,pandas,lambda,list-comprehension,Python,Pandas,Lambda,List Comprehension,这里的第一个问题可能有点混乱 所以我有一个这样的数据框: A B 1: 'a' 'aa' 2: 'b' NaN 3: 'c' NaN 4: 'd' 'dd' 我已经创建了一个列表: lst=[] 如果列B的值为NaN,我想将列A中的值附加到此列表中,在这种情况下,也称为获得['B','c'] 循环确实可以工作,但是有没有一种优雅的方法(例如使用lam

这里的第一个问题可能有点混乱

所以我有一个这样的数据框:

           A          B
1:        'a'       'aa'
2:        'b'       NaN
3:        'c'       NaN
4:        'd'       'dd'
我已经创建了一个列表:

lst=[]
如果
列B
的值为
NaN
,我想将
列A
中的值附加到此
列表中,在这种情况下,也称为获得
['B','c']

循环确实可以工作,但是有没有一种优雅的方法(例如使用lambdas)可以做到这一点

谢谢

用于筛选和删除

lst = df.loc[df['B'].isnull(), 'A'].tolist()
print (lst)
["'b'", "'c'"]

详情:

print (df['B'].isnull())
1:    False
2:     True
3:     True
4:    False
Name: B, dtype: bool

print (df.loc[df['B'].isnull(), 'A'])
2:    'b'
3:    'c'
Name: A, dtype: object

print (df.loc[df['B'].isnull(), 'A'].str.strip("'"))
2:    b
3:    c
Name: A, dtype: object
用于筛选和删除

lst = df.loc[df['B'].isnull(), 'A'].tolist()
print (lst)
["'b'", "'c'"]

详情:

print (df['B'].isnull())
1:    False
2:     True
3:     True
4:    False
Name: B, dtype: bool

print (df.loc[df['B'].isnull(), 'A'])
2:    'b'
3:    'c'
Name: A, dtype: object

print (df.loc[df['B'].isnull(), 'A'].str.strip("'"))
2:    b
3:    c
Name: A, dtype: object