Page 72 - Informatics_Practices_Fliipbook_Class12
P. 72

These rows will have the value NaN in the column Unemployment Rate of the concatenated DataFrame, as shown
        below:
         >>> # Concatenate DataFrames row-wise
         >>> gdpDF = pd.concat([gdpDF1, gdpDF2], ignore_index=True)
         >>> print(gdpDF)
                 Year  Gross Domestic Product  Inflation Rate  Unemployment Rate
              0  2018                    21.3             2.1                NaN
              1  2019                    22.6             1.8                NaN
              2  2020                    20.9             2.5                NaN
              3  2021                    23.2             2.5                4.2
              4  2022                    24.6             2.0                4.4
        By default, the method pd.concat() concatenates the two Dataframes rows wise (axis = 0). However, we can
        also concatenate the DataFrames column wise by setting keyword argument axis = 1. For example, suppose, the
        first Dataframe contains information regarding expenses (Rent, Utilities, Groceries) for first 4 months of the year and
        second Dataframe contains information regarding monthly income and investments (Salary, Bonus, Investments) for
        the same month as shown below:
         >>> import pandas as pd
         >>> # Create the first DataFrame for monthly expenses
         >>> data1 = {'Month': ['January', 'February', 'March', 'April'],
                       'Rent': [1200, 1200, 1200, 1300],
                       'Utilities': [150, 170, 160, 165],
                       'Groceries': [300, 350, 320, 350]}
         >>> df1 = pd.DataFrame(data1)

         >>> # Create the second DataFrame for monthly income
         >>> data2 = {'Month_df2': ['January', 'February', 'March', 'April'],
                       'Salary': [4000, 4000, 4000, 4000],
                       'Bonus': [500, 600, 550, 600],
                       'Investments': [1000, 900, 1100, 900]}
         >>> df2 = pd.DataFrame(data2)

              print("Monthly Expenses:\n", df1,"\n")
              print("Monthly Income:\n", df2, '\n')
              # Concatenate DataFrames column-wise
              df = pd.concat([df1, df2], axis=1)
              print(df.head())
        output:
              Monthly Expenses:
                     Month  Rent  Utilities  Groceries
              0    January  1200        150        300
              1   February  1200        170        350
              2      March  1200        160        320
              3      April  1300        165        350

              Monthly Income:
                 Month_df2  Salary  Bonus  Investments
              0    January    4000    500         1000
              1   February    4000    600          900
              2      March    4000    550         1100
              3      April    4000    600          900

                    Month  Rent  Utilities  Groceries Month_df2  Salary  Bonus  Investments
              0   January  1200        150        300   January    4000    500         1000
              1  February  1200        170        350  February    4000    600          900
              2     March  1200        160        320     March    4000    550         1100
              3     April  1300        165        350     April    4000    600          900

          58   Touchpad Informatics Practices-XII
   67   68   69   70   71   72   73   74   75   76   77