JohnnieL
JohnnieL

Reputation: 1231

python pandas dataframe union merge list into index

I have a list of values and I want to guarantee that all values exist in the index of a data frame.

I have achieved this using the following but want to know can i do this in a simpler way? I have tried various ~ ... .isin constructions but failed to get what I want Thanks

#df1 is my data frame 
df1=pd.DataFrame({'i':[1, 3, 5, 7], 
                 'a':[2, 4, 6, 8]})
df1.set_index(['i'], inplace=True)
df1

enter image description here

# l is the list containing index values
# i make this a data frame with an index but no columns so i can use merge
l=[3, 4, 5]
df=pd.DataFrame(l)
df.set_index([0], inplace=True)
df.index.names=['i']
df

enter image description here

Use merge to add any missing index values

df1.merge(df, how='outer', on='i', sort=True)

enter image description here

Upvotes: 1

Views: 695

Answers (1)

ALollz
ALollz

Reputation: 59579

Use reindex together with pd.Index.union (assuming you don't have any duplicate values for the DataFrame index or duplicates in l)

import pandas as pd

df1 = pd.DataFrame({'i':[1, 3, 5, 7], 'a':[2, 4, 6, 8]}).set_index('i')
l = [3, 4, 5]

df1 = df1.reindex(df1.index.union(l))
#     a
#i     
#1  2.0
#3  4.0
#4  NaN
#5  6.0
#7  8.0

Upvotes: 2

Related Questions