Python Dataframe 将索引映射到列表的一列以提取元素
Python Dataframe map indexes to a column of lists for extracting elements
我有一个由列表作为元素组成的数据框。另外,我有一个已知索引列表。现在我想提取每行中那些索引中存在的元素。
我的代码:
df = pd.DataFrame({'A':[[7,8],[4,5,NaN],[NaN,1,9]],'match_idx':[1,0,NaN]})
df
A match_idx
0 [7, 8] 1
1 [4, 5, nan] 0
2 [nan, 1, 9] NaN
# in each row, let's find the values located in the match_idx position
当前解决方案:
df['A_element'] = df.apply(lambda x: x['A'][x['match_idx']] if ~x['match_idx'].isnan() else np.nan,axis=1)
AttributeError: 'float' object has no attribute 'isnan'
预期解决方案:
df =
A match_idx A_element
0 [7, 8] 1 8
1 [4, 5, nan] 0 4
2 [nan, 1, 9] NaN NaN
对于 tet 非缺失值,使用 notna
并将索引转换为整数:
df['A_element'] = [a[int(i)] if pd.notna(i) else np.nan
for a, i in zip(df['A'], df['match_idx'])]
或者:
df['A_element'] = df.apply(lambda x: x['A'][int(x['match_idx'])]
if pd.notna(x['match_idx']) else np.nan,axis=1)
print (df)
A match_idx A_element
0 [7, 8] 1.0 8.0
1 [4, 5, nan] 0.0 4.0
2 [nan, 1, 9] NaN NaN
我有一个由列表作为元素组成的数据框。另外,我有一个已知索引列表。现在我想提取每行中那些索引中存在的元素。 我的代码:
df = pd.DataFrame({'A':[[7,8],[4,5,NaN],[NaN,1,9]],'match_idx':[1,0,NaN]})
df
A match_idx
0 [7, 8] 1
1 [4, 5, nan] 0
2 [nan, 1, 9] NaN
# in each row, let's find the values located in the match_idx position
当前解决方案:
df['A_element'] = df.apply(lambda x: x['A'][x['match_idx']] if ~x['match_idx'].isnan() else np.nan,axis=1)
AttributeError: 'float' object has no attribute 'isnan'
预期解决方案:
df =
A match_idx A_element
0 [7, 8] 1 8
1 [4, 5, nan] 0 4
2 [nan, 1, 9] NaN NaN
对于 tet 非缺失值,使用 notna
并将索引转换为整数:
df['A_element'] = [a[int(i)] if pd.notna(i) else np.nan
for a, i in zip(df['A'], df['match_idx'])]
或者:
df['A_element'] = df.apply(lambda x: x['A'][int(x['match_idx'])]
if pd.notna(x['match_idx']) else np.nan,axis=1)
print (df)
A match_idx A_element
0 [7, 8] 1.0 8.0
1 [4, 5, nan] 0.0 4.0
2 [nan, 1, 9] NaN NaN