Jack
Jack

Reputation: 1754

How to append dataframe index by union an array in pandas?

Example

df:

   col_a  col_b 
a   2      3
b   4      4
c   3      2

array: np.array([c,d,e])

Expected

The array unions the dataframe index, it returns the dataframe as follow:

   col_a  col_b 
a   2      3
b   4      4
c   3      2
d   NaN    NaN
e   NaN    NaN

Try

I use pd.concat([pd.Series(array),df],axis=1),but failed. How can I get it?

Upvotes: 4

Views: 1040

Answers (5)

Roy2012
Roy2012

Reputation: 12523

One more way to do that:

a = np.array(["c","d","e"])

df.reindex(set(df.index).union(a)).sort_index()

The result is:

   col_a  col_b
a    2.0    3.0
b    4.0    4.0
c    3.0    2.0
d    NaN    NaN
e    NaN    NaN

Upvotes: 0

Shubham Sharma
Shubham Sharma

Reputation: 71687

Instead of using pd.concat, one alternative way is to use DataFrame.combine_first:

a = np.array(['c', 'd', 'e'])
df = df.combine_first(pd.DataFrame(index=a))

# print(df)

   col_a  col_b
a    2.0    3.0
b    4.0    4.0
c    3.0    2.0
d    NaN    NaN
e    NaN    NaN

Upvotes: 2

Ch3steR
Ch3steR

Reputation: 20659

You can use pd.Index.union with df.reindex

a = np.array(['c', 'd', 'e'])
df.reindex(df.index.union(a),axis=0)
   col_a  col_b
a    2.0    3.0
b    4.0    4.0
c    3.0    2.0
d    NaN    NaN
e    NaN    NaN

Upvotes: 2

Dishin H Goyani
Dishin H Goyani

Reputation: 7713

You can use numpy.union1d like

idx = np.array(['c','d','e'])

df = df.reindex(np.union1d(df.index, idx))
df
   col_a  col_b
a    2.0    3.0
b    4.0    4.0
c    3.0    2.0
d    NaN    NaN
e    NaN    NaN

Upvotes: 1

Daniel Labbe
Daniel Labbe

Reputation: 2019

Considering the sample data:

import pandas as pd
data = {'col_a': [2,4,3], 'col_b': [3,4,2]}
index = ['a', 'b', 'c']
df = pd.DataFrame(data, index)

This piece of code does what you need:

df = df.append(pd.Series(name='d'))
df = df.append(pd.Series(name='e'))

With the following output:

df
a   2.0     3.0
b   4.0     4.0
c   3.0     2.0
d   NaN     NaN
e   NaN     NaN

Upvotes: 0

Related Questions