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