python - 来自包含 NaN 值的数据的 Pandas 数据框列
问题描述
以下代码将给定的 pandas 列FEAT
转换为名为STREAM
. 只要原始数据帧中没有 NaN 值,该程序就可以工作。如果是这种情况,则会发生以下异常:ValueError: Length of values does not match length of index
. 我需要将 NaN 值推送到新列。可行吗?这是失败的代码选项:
import pandas as pd
import numpy as np
data = {
'FEAT': [8, 15, 7, np.nan, 5, 2, 11, 15]
}
customer = pd.DataFrame(data)
customer = pd.DataFrame(data, index=['June', 'Robert', 'Lily', 'David', 'Bob', 'Sally', 'Mia', 'Luis'])
#create binary variable STREAM 0:mainstream 1:avantgarde
stream_0 = [1, 3, 5, 8, 10, 12, 14]
stream_1 = [2, 4, 6, 7, 9, 11, 13, 15]
# convert FEAT to list_0
list_0 = customer['FEAT'].values.tolist()
# create a list of length = len(customer) whose elements are:
# 0 if the value of 'FEAT' is in stream_0
# 1 if the value of 'FEAT' is in stream_1
L = []
for i in list_0:
if i in stream_0:
L.append(0)
elif i in stream_1:
L.append(1)
# convert the list to a new column of customer df
customer['STREAM'] = L
print(customer)
解决方案
问题是你错过了一个else
块,所以当一个值(比如NaN
)既不在stream_0
也不stream_1
做任何事情时,这会导致 L 的元素少于customer
.
在这里循环是不必要的,np.select
可以处理列的创建。该default
参数将处理该else
块。
customer['STREAM'] = np.select([customer.FEAT.isin(stream_0), customer.FEAT.isin(stream_1)],
[0, 1], default=np.NaN)
FEAT STREAM
June 8.0 0.0
Robert 15.0 1.0
Lily 7.0 1.0
David NaN NaN
Bob 5.0 0.0
Sally 2.0 1.0
Mia 11.0 1.0
Luis 15.0 1.0
您还可以映射几个值,不在其中的所有值都是NaN
d = {key: value for l,value in zip([stream_0, stream_1], [0,1]) for key in l}
customer['STREAM'] = customer['FEAT'].map(d)
dict 使用理解来创建键值对。对于其中的每个键,stream_0
我们为其分配一个值0
,对于其中的每个键,stream_1
我们为其分配一个值1
。理解有点复杂,所以更容易理解的方法是分别创建每个字典,然后将它们组合起来。
d_1 = {k: 0 for k in stream_0}
d_2 = {k: 1 for k in stream_1}
d = {**d_1, **d_2} # Combine
#{1: 0, 2: 1, 3: 0, 4: 1, 5: 0, 6: 1, 7: 1,
# 8: 0, 9: 1, 10: 0, 11: 1, 12: 0, 13: 1, 14: 0, 15: 1}
推荐阅读
- xsd - XSD 验证具有不同内容的相同元素名称
- azure - 无法在 Azure CLI 中为 Azure Function 设置 functionAppScaleLimit 设置
- php - php:将变量(用户从单选按钮选择)获取到另一个文件
- amazon-web-services - 如何修复在本地服务器上正常运行但部署到 AWS 后无法运行的程序?
- javascript - 我应该为 Electron 应用程序中使用的授权调用使用什么重定向 URI?
- java - 我可以在java中的泛型函数中为泛型对象的特定属性设置值吗?
- python - 如何解决第二个脚本中的 EOF 问题(虚线下方)?
- javascript - 如何使用 appendChild 将多个元素添加到父元素?
- bash - 使用 Bash 成对列出文件
- ios - iOS 设备中 admob 广告的先决条件问题