遇到一个需求,现在假设有一个这样的df,c是金额,d是月份

C            D

nan      321323                          

0          3132                                 

0          nan

nan       nan

有两个规则:1.金额不能为nan     2.金额为0月份的时候如果月份也为nan那么这条记录无效

思路:判断c不能为nan,筛选 : df1['C'].notna()         然后把符合c为0 以及d为nan的那列搞死:    df2.drop(df2[(df2['C']==0) & np.isnan(df2['D'])].index)

# -*- coding: utf-8 -*
import numpy as np
import pandas as pd
from pandas import Series,DataFrame

#第一个是放在df里面的随机数据,第二个是索引,也叫行,第三个叫列
df1=pd.DataFrame(
np.random.randn(4,4),
index=list('abcd'),
columns=list('ABCD')
)
print('before..............')
df1.iloc[0,2]=np.nan
df1.iloc[1,2]=0
df1.iloc[2,2]=0
df1.iloc[2,3]=np.nan
print(df1)
str1 = "A"

print('after..............')
#首先判断c不能为nan df2
= df1[(df1['C'].notna())] df2 = df2.drop(df2[(df2['C']==0) & np.isnan(df2['D'])].index) print(df2)

 

 posted on 2019-07-09 16:09  WU大雄  阅读(388)  评论(0编辑  收藏  举报