Python 3.x 如何向缺少的值i添加值
我有一个数据框Python 3.x 如何向缺少的值i添加值,python-3.x,pandas,numpy,Python 3.x,Pandas,Numpy,我有一个数据框 Software Product Case Number Num of days MDM9607.LE.1.0 2774904 -19.13888889 MDM9607.LE.1.0 2774203 -19.60069444 MDM9607.LE.1.0 2768088 -24.81597222 MDM9607.LE.1.0 27675
Software Product Case Number Num of days
MDM9607.LE.1.0 2774904 -19.13888889
MDM9607.LE.1.0 2774203 -19.60069444
MDM9607.LE.1.0 2768088 -24.81597222
MDM9607.LE.1.0 2767500 -25.0125
MDM9607.LE.1.0 2764617 -26.67916667
MDM9607.LE.1.0 2766991 -25.17430556
MDM9607.LE.1.0 2765696
MDM9607.LE.1.0 2764204
MDM9607.LE.1.0 2764199
MDM9607.LE.1.0 2774434 365
MDM9607.LE.1.0 2769029 377
MDM9607.LE.1.0 2764195 380
MDM9607.LE.1.0 2763721 25
MDM9607.LE.1.0 2770456 380
MDM9607.LE.1.0 2768423
所需输出条件:
If:
f9['Num of days'] > 365 than print L
f9['Num of days'] < 365 than print N
f9['Num of days'] == NaN than print U
我使用的Dataframe包含一些缺失值,缺失值应打印为未知值,但我使用的是一些逻辑,如果列大于365,则应打印为“L”使用-首先测试缺失值,然后根据条件,最后使用参数默认值
:
m1 = f9['Num of days'].isna()
m2 = f9['Num of days'] > 365
f9['Status'] = np.select([m1, m2], ['U','L'], default='N')
print (f9)
Software Product Case Number Num of days Status
0 MDM9607.LE.1.0 2774904 -19.138889 N
1 MDM9607.LE.1.0 2774203 -19.600694 N
2 MDM9607.LE.1.0 2768088 -24.815972 N
3 MDM9607.LE.1.0 2767500 -25.012500 N
4 MDM9607.LE.1.0 2764617 -26.679167 N
5 MDM9607.LE.1.0 2766991 -25.174306 N
6 MDM9607.LE.1.0 2765696 NaN U
7 MDM9607.LE.1.0 2764204 NaN U
8 MDM9607.LE.1.0 2764199 NaN U
9 MDM9607.LE.1.0 2774434 365.000000 N
10 MDM9607.LE.1.0 2769029 377.000000 L
11 MDM9607.LE.1.0 2764195 380.000000 L
12 MDM9607.LE.1.0 2763721 25.000000 N
13 MDM9607.LE.1.0 2770456 380.000000 L
14 MDM9607.LE.1.0 2768423 NaN U
使用-首先测试缺少的值,然后根据条件,最后使用参数默认值
:
m1 = f9['Num of days'].isna()
m2 = f9['Num of days'] > 365
f9['Status'] = np.select([m1, m2], ['U','L'], default='N')
print (f9)
Software Product Case Number Num of days Status
0 MDM9607.LE.1.0 2774904 -19.138889 N
1 MDM9607.LE.1.0 2774203 -19.600694 N
2 MDM9607.LE.1.0 2768088 -24.815972 N
3 MDM9607.LE.1.0 2767500 -25.012500 N
4 MDM9607.LE.1.0 2764617 -26.679167 N
5 MDM9607.LE.1.0 2766991 -25.174306 N
6 MDM9607.LE.1.0 2765696 NaN U
7 MDM9607.LE.1.0 2764204 NaN U
8 MDM9607.LE.1.0 2764199 NaN U
9 MDM9607.LE.1.0 2774434 365.000000 N
10 MDM9607.LE.1.0 2769029 377.000000 L
11 MDM9607.LE.1.0 2764195 380.000000 L
12 MDM9607.LE.1.0 2763721 25.000000 N
13 MDM9607.LE.1.0 2770456 380.000000 L
14 MDM9607.LE.1.0 2768423 NaN U
np.where(f9['Num of days']>365,'L','NL','u')
:为什么里面有一个'u'
?这是导致您出现错误的原因,不符合您对问题的描述。U表示缺少值选项卡和空间的组合中有哪些是重要的?np.where(f9['Num of days']>365,'L','NL','U')
:为什么在其中有一个'U'
?这是导致您出现错误的原因,并且不符合您对问题的描述。U缺少值选项卡和空间的组合中有哪些是重要的?嗨,jezrael,我在下面使用了代码import pandas作为pd import numpy作为np f8=pd.read_excel(r“”)f9=f8.fillna('0/0/0000 0:00:00 AM')f10=pd.to_datetime(f9['react EOL Date'],格式=“%m\%d\%Y%I:%m:%S%p”,错误为“强制”)打印(f10),但我仍然得到一列空的rowshi jezrael,我使用下面的代码导入熊猫作为pd导入numpy作为np f8=pd。读取excel(r“”)f9=f8。填充('0/0/0000 0:00:00 AM')f10=pd.to_datetime(f9['Actual EOL Date',格式=“%m\%d\%Y:%m:%S%p”,错误为“强制”)打印(f10)但我还是得到了一个空行的列
m1 = f9['Num of days'].isna()
m2 = f9['Num of days'] > 365
f9['Status'] = np.select([m1, m2], ['U','L'], default='N')
print (f9)
Software Product Case Number Num of days Status
0 MDM9607.LE.1.0 2774904 -19.138889 N
1 MDM9607.LE.1.0 2774203 -19.600694 N
2 MDM9607.LE.1.0 2768088 -24.815972 N
3 MDM9607.LE.1.0 2767500 -25.012500 N
4 MDM9607.LE.1.0 2764617 -26.679167 N
5 MDM9607.LE.1.0 2766991 -25.174306 N
6 MDM9607.LE.1.0 2765696 NaN U
7 MDM9607.LE.1.0 2764204 NaN U
8 MDM9607.LE.1.0 2764199 NaN U
9 MDM9607.LE.1.0 2774434 365.000000 N
10 MDM9607.LE.1.0 2769029 377.000000 L
11 MDM9607.LE.1.0 2764195 380.000000 L
12 MDM9607.LE.1.0 2763721 25.000000 N
13 MDM9607.LE.1.0 2770456 380.000000 L
14 MDM9607.LE.1.0 2768423 NaN U