1

I have two data sets with policy numbers:

  1. df_only1['Policy_no']
  2. df_only2['Policy_no']

I want to append the policy numbers into one dataset: df_relevant_pols['Policy_no'] keeping the dataformat (string although they are numerical) and the column name so that I can then later access the list with isin(df_relevant_pols['Policy_no']).

pbn
  • 112
  • 10
wuz
  • 483
  • 3
  • 16
  • Please take a look at [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) and provide a [mcve] including sample input, sample output, and code for what you've tried so far. From the basic description, it sounds like a straightforward `pd.merge` – G. Anderson Nov 15 '19 at 16:53

2 Answers2

1

It seems you need panda's concat to perform what you are trying to achieve. Does this help?

import pandas as pd
data_only1 = {'Policy_no':[100,101,102,103]}
data_only2 = {'Policy_no':[110,111,112,113]}

df_only1 = pd.DataFrame(data_only1)
df_only2 = pd.DataFrame(data_only2)

df_relevant_pols = pd.concat([df_only1,df_only2])
df_relevant_pols['Policy_no'] = df_relevant_pols['Policy_no'].astype(str)
print(df_relevant_pols)

Output:

   Policy_no
0        100
1        101
2        102
3        103
0        110
1        111
2        112
3        113

Of course you can perform .reset_index() if you wish to not keep the original index from both dataframes.

Celius Stingher
  • 17,835
  • 6
  • 23
  • 53
0

If you just want to use them to check with isin(df_relevant_pols['Policy_no']), you can create a set, there are faster than using a list when comparing using is_in().

It could look like so:

df1 = pd.DataFrame({'Policy_no':[0,1,3,6]})
df2 = pd.DataFrame({'Policy_no':[0,2,4]})

policy_numbers = set(np.append(df1.Policy_no.values, df2.Policy_no.values))
# Result: {0, 1, 2, 3, 4, 6}

Then when you want to filter:

df3 = pd.DataFrame({'Policy_no':[i for i in range(10)]})
df3[df3.Policy_no.isin(policy_numbers)]

   Policy_no
0          0
1          1
2          2
3          3
4          4
6          6
vlemaistre
  • 3,301
  • 13
  • 30