小能豆

Pandas.loc() return empty values for unavailable indexes

python

I have a dataframe like this:

ind,l1,l2 = [0,1,0],[1,1,0],[0,0,1]
df = pd.DataFrame([ind,l1,l2],columns=[['C1','C2','C3']])
df.index=[['A','C','E']]

enter image description here

I want to use df.loc([[‘A’,’B’,’C’,’D’,’E’]]) so that the unavailable indexes B & D returns nan values.

enter image description here

Can anyone help me with this?


阅读 73

收藏
2023-12-07

共1个答案

小能豆

Change loc to reindex

df.reindex([['A','B','C','D','E']], axis=0)
2023-12-07