我正在尝试使用pd.melt将3列融化为一个分类列。目前,数据帧看起来像这样。
id1 Plane Car Boat
0 123 None None None
1 124 Plane None None
2 125 None None Boat
在某个时刻,我用NaN代替None,但我不确定在熔化之前是否有必要这样做。我的目标是有一个类别列,它列出了车辆的类型,只有当所有列都为空时才使用None。
id1 Type
0 123 None
1 124 Plane
2 125 Boat
我想出的代码是:
df = pd.melt(df, id_vars=['id1'], var_name='Type')
我遇到的问题是,它将我的数据帧中的观测值增加了三倍。我可以过滤掉Type=None的行,但这会删除id1=123等原始列全部为None的数据。
id1 Type
0 123 None
1 123 None
2 123 None
3 124 Plane
4 124 None
5 124 None
有没有一种有效的方法来处理熔体?或者,我需要循环遍历数据并用条件语句写入一个新的数据帧?
你可以这样做。使用reindex来取回那些丢失的id值。
df1 = df.replace('None',np.nan).set_index('id1')
df1.stack().reset_index(level=1, drop=True).reindex(df1.index)
输出:
id1
123 NaN
124 Plane
125 Boat
dtype: object
您可以使用补漏值,然后按位置选择第一列-按iloc
:
df = df.replace('None', np.nan)
df = df.set_index('id1').bfill(axis=1).iloc[:, 0].rename('Type').reset_index()
print (df)
id1 Type
0 123 NaN
1 124 Plane
2 125 Boat
如果性能很重要,可以在numpy
中使用具有两个更改的正当功能:
def justify(a, invalid_val=0, axis=1, side='left'):
"""
Justifies a 2D array
Parameters
----------
A : ndarray
Input array to be justified
axis : int
Axis along which justification is to be made
side : str
Direction of justification. It could be 'left', 'right', 'up', 'down'
It should be 'left' or 'right' for axis=1 and 'up' or 'down' for axis=0.
"""
if invalid_val is np.nan:
mask = pd.notnull(a) <- change to notnull
else:
mask = a!=invalid_val
justified_mask = np.sort(mask,axis=axis)
if (side=='up') | (side=='left'):
justified_mask = np.flip(justified_mask,axis=axis)
out = np.full(a.shape, invalid_val, dtype=object) <- change dtype to object
if axis==1:
out[justified_mask] = a[mask]
else:
out.T[justified_mask.T] = a.T[mask.T]
return out
numpy中也有相同的想法-新的数据帧由assign
由1d array
:创建
arr = df.replace('None', np.nan).values[:, 1:]
out = justify(arr, invalid_val=np.nan)[:, 0]
print (out)
[nan 'Plane' 'Boat']
df = df[['id1']].assign(Type=out)
print (df)
id1 Type
0 123 NaN
1 124 Plane
2 125 Boat
无需使用pd.melt
,只需使用以下内容:
df=df.replace('None',pd.np.nan)
df['final']=df.apply(lambda a: pd.Series(a[1:]).dropna()[0] if len(pd.Series(a[1:]).dropna())!=0 else pd.np.nan,axis=1)
print(df[['id1','final']])
输出:
id1 final
0 123 NaN
1 124 Plane
2 125 Boat
您可以通过从转置数据帧中获取相关行,将None转换为空字符串并对值求和来实现此结果,如下所示。
输入:
from io import StringIO
df = pd.read_table(StringIO(""" id1 Plane Car Boat
0 123 None None None
1 124 Plane None None
2 125 None None Boat"""), delimiter="s+")
df
Out[229]:
id1 Plane Car Boat
0 123 None None None
1 124 Plane None None
2 125 None None Boat
代码:
df["Type"] = df.T.iloc[1:].replace({"None":""}).sum().replace({"":"None"})
df.drop(columns=['Plane', 'Car', 'Boat'], inplace=True)
输出:
df
Out[231]:
id1 Type
0 123 None
1 124 Plane
2 125 Boat