How to add new columns to dataframe with value taken from another dataframe?

Kunitsyn Artsiom

I have two dataframes: df1.head():

    nazwa województwa   gmina nazwa gminy  rodzaj gminy
0  Zachodniopomorskie  320101   Białogard             1
1  Zachodniopomorskie  320101   Białogard             1
2  Zachodniopomorskie  320101   Białogard             1
3  Zachodniopomorskie  320101   Białogard             1
4  Zachodniopomorskie  320101   Białogard             1

and kts_df.head():

               name         type        KTS_code TERYT_code
0            Polska      COUNTRY  10000000000000       None
1           Bochnia  RURAL_GMINA  10011212001022    1201022
2           Drwinia  RURAL_GMINA  10011212001032    1201032
3         Iwanowice  RURAL_GMINA  10011212006032    1206032
4  Lipnica Murowana  RURAL_GMINA  10011212001042    1201042

Currently to add new column to df1 I am using

df['kts'] = df.apply(lambda row: self.get_kts_code(row, kts_df), axis=1) where

    def get_kts_code(self, row, kts_df: DataFrame) -> str:
            """Get the KTS code of each miasto/wieś."""
            gmina_types = {
                '1': AdministrativeUnitType.URBAN_GMINA,
                '2': AdministrativeUnitType.RURAL_GMINA,
                '4': AdministrativeUnitType.MIXED_GMINA,
                '5': AdministrativeUnitType.RURAL_AREA,
                '8': AdministrativeUnitType.DISTRICT,
                '9': AdministrativeUnitType.DELEGATION,
            }
            nazwa_gminy = row['nazwa gminy']
            gmina_type = gmina_types[str(row['rodzaj gminy'])]
            teryt = kts_df['TERYT_code'].str.contains(str(row['gmina']))
            kts_code = kts_df.loc[
                (kts_df['name'] == nazwa_gminy) & (kts_df['type'] == gmina_type) & (teryt)
            ]
            kts_code = kts_code['KTS_code'].values[0]
            return kts_code

This code works well, but to process df1 with about 200k rows it takes about an hour, too slow. Probably there can be another way to quickly find correct kts_code from kts_df for each row of df1?

Into Numbers

I'm not sure if I've got your demand right, but you could try the following:

  • create a df with the corresponding gmina_type mapping
  • join kts_df with gmina_types_df to get the gmina IDs
  • join df1 with the enriched kts_df

Code Example:

gmina_types = {
    'id': [1, 2, 3, 4, 5],
    'type': ['URBAN_GMINA', 'RURAL_GMINA', 'MIXED_GMINA', 'RURAL_AREA', 'DISTRICT']
}

gmina_types_df = pd.DataFrame.from_dict(gmina_types)

kts_df = kts_df.join(gmina_types_df.set_index('type'), on='type')
df1 = df1.join(kts_df.set_index('id'), on='rodzaj gminy')

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

R: create new dataframe rows are columns from another dataframe

From Dev

Fill multiple columns with value from another dataframe

From Dev

How to create new columns in dataframe based on conditional matches on another dataframe?

From Dev

Add new column to long dataframe from another dataframe?

From Dev

Name of the dataframe from which the minimum value is taken

From Dev

How can I add a column from one dataframe to another dataframe?

From Dev

Python: add columns to dataframe from another with matching "vlookup"

From Dev

Python: add columns to dataframe from another with matching "vlookup"

From Dev

How to add or combine two columns into another one in a dataframe if they meet a condition

From Dev

How to use custom function to add multiple new columns to dataframe?

From Dev

Pandas dataframe: how to apply describe() to each group and add to new columns?

From Dev

How to add numpy matrix as new columns for pandas dataframe?

From Dev

Add a different number of columns to dataframe depending on value from row in R

From Dev

How to add a new column with day of week based on another in dataframe?

From Dev

Spark add new column to dataframe with value from previous row

From Dev

Replacing Columns from one dataframe with columns from another dataframe in pandas

From Dev

Replacing Columns from one dataframe with columns from another dataframe in pandas

From Dev

How to add column in Dataframe base on the value of other Columns spark

From Dev

How to add a Spark Dataframe to the bottom of another dataframe?

From Java

If one dataframe value exists in another dataframe, then get a value from the dataframe

From Dev

Change the value from another dataframe

From Dev

remove a row from a dataframe if any row value is in another dataframe , with dataframes having multiple columns

From Dev

Pandas populate new dataframe column based on matching columns in another dataframe

From Dev

How to create a new dataframe column with shifted values from another column?

From Dev

How to add multiple columns to a dataframe from a custom function in R

From Dev

How to Add Dataframe name to Columns from Multiple Dataframes

From Dev

R - Add a new column to a dataframe using matching values of another dataframe

From Dev

Python: In DataFrame, add value in a new column for row with highest value in another column and string identical in a third one

From Dev

How to add one number to another taken from mysql in php?

Related Related

  1. 1

    R: create new dataframe rows are columns from another dataframe

  2. 2

    Fill multiple columns with value from another dataframe

  3. 3

    How to create new columns in dataframe based on conditional matches on another dataframe?

  4. 4

    Add new column to long dataframe from another dataframe?

  5. 5

    Name of the dataframe from which the minimum value is taken

  6. 6

    How can I add a column from one dataframe to another dataframe?

  7. 7

    Python: add columns to dataframe from another with matching "vlookup"

  8. 8

    Python: add columns to dataframe from another with matching "vlookup"

  9. 9

    How to add or combine two columns into another one in a dataframe if they meet a condition

  10. 10

    How to use custom function to add multiple new columns to dataframe?

  11. 11

    Pandas dataframe: how to apply describe() to each group and add to new columns?

  12. 12

    How to add numpy matrix as new columns for pandas dataframe?

  13. 13

    Add a different number of columns to dataframe depending on value from row in R

  14. 14

    How to add a new column with day of week based on another in dataframe?

  15. 15

    Spark add new column to dataframe with value from previous row

  16. 16

    Replacing Columns from one dataframe with columns from another dataframe in pandas

  17. 17

    Replacing Columns from one dataframe with columns from another dataframe in pandas

  18. 18

    How to add column in Dataframe base on the value of other Columns spark

  19. 19

    How to add a Spark Dataframe to the bottom of another dataframe?

  20. 20

    If one dataframe value exists in another dataframe, then get a value from the dataframe

  21. 21

    Change the value from another dataframe

  22. 22

    remove a row from a dataframe if any row value is in another dataframe , with dataframes having multiple columns

  23. 23

    Pandas populate new dataframe column based on matching columns in another dataframe

  24. 24

    How to create a new dataframe column with shifted values from another column?

  25. 25

    How to add multiple columns to a dataframe from a custom function in R

  26. 26

    How to Add Dataframe name to Columns from Multiple Dataframes

  27. 27

    R - Add a new column to a dataframe using matching values of another dataframe

  28. 28

    Python: In DataFrame, add value in a new column for row with highest value in another column and string identical in a third one

  29. 29

    How to add one number to another taken from mysql in php?

HotTag

Archive