|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 2, |
| 6 | + "id": "97177ccf", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [], |
| 9 | + "source": [ |
| 10 | + "import pandas as pd\n", |
| 11 | + "import numpy as np" |
| 12 | + ] |
| 13 | + }, |
| 14 | + { |
| 15 | + "cell_type": "code", |
| 16 | + "execution_count": 8, |
| 17 | + "id": "8cf560c1", |
| 18 | + "metadata": {}, |
| 19 | + "outputs": [ |
| 20 | + { |
| 21 | + "name": "stdout", |
| 22 | + "output_type": "stream", |
| 23 | + "text": [ |
| 24 | + "DataFrame from dictionary:\n", |
| 25 | + " Product Price\n", |
| 26 | + "P1 Apple 1.00\n", |
| 27 | + "P2 Banana 0.50\n", |
| 28 | + "P3 Orange 0.75\n" |
| 29 | + ] |
| 30 | + } |
| 31 | + ], |
| 32 | + "source": [ |
| 33 | + "# From a dictionary\n", |
| 34 | + "data = {\n", |
| 35 | + " 'Product': ['Apple', 'Banana', 'Orange'], \n", |
| 36 | + " 'Price': [1.0, 0.5, 0.75]\n", |
| 37 | + " }\n", |
| 38 | + "indexx = ['P1', 'P2', 'P3']\n", |
| 39 | + "df = pd.DataFrame(data, index=indexx)\n", |
| 40 | + "print(\"DataFrame from dictionary:\\n\", df)" |
| 41 | + ] |
| 42 | + }, |
| 43 | + { |
| 44 | + "cell_type": "code", |
| 45 | + "execution_count": 14, |
| 46 | + "id": "d80bd1cf", |
| 47 | + "metadata": {}, |
| 48 | + "outputs": [ |
| 49 | + { |
| 50 | + "name": "stdout", |
| 51 | + "output_type": "stream", |
| 52 | + "text": [ |
| 53 | + "Merged DataFrame:\n", |
| 54 | + " Product Price Region\n", |
| 55 | + "0 Apple 1.00 North\n", |
| 56 | + "1 Banana 0.50 South\n", |
| 57 | + "2 Books NaN North\n", |
| 58 | + "3 Orange 0.75 NaN\n" |
| 59 | + ] |
| 60 | + } |
| 61 | + ], |
| 62 | + "source": [ |
| 63 | + "# Creating another DataFrame\n", |
| 64 | + "data2 = {\n", |
| 65 | + " 'Product': ['Apple', 'Banana','Books'], \n", |
| 66 | + " 'Region': ['North', 'South','North']\n", |
| 67 | + " }\n", |
| 68 | + "df2 = pd.DataFrame(data2)\n", |
| 69 | + "\n", |
| 70 | + "# Merging DataFrames\n", |
| 71 | + "merged = pd.merge(df, df2, on='Product', how='outer')\n", |
| 72 | + "print(\"Merged DataFrame:\\n\", merged)\n", |
| 73 | + "# how{‘left’, ‘right’, ‘outer’, ‘inner’, ‘cross’}, default ‘inner’" |
| 74 | + ] |
| 75 | + } |
| 76 | + ], |
| 77 | + "metadata": { |
| 78 | + "kernelspec": { |
| 79 | + "display_name": "Python 3", |
| 80 | + "language": "python", |
| 81 | + "name": "python3" |
| 82 | + }, |
| 83 | + "language_info": { |
| 84 | + "codemirror_mode": { |
| 85 | + "name": "ipython", |
| 86 | + "version": 3 |
| 87 | + }, |
| 88 | + "file_extension": ".py", |
| 89 | + "mimetype": "text/x-python", |
| 90 | + "name": "python", |
| 91 | + "nbconvert_exporter": "python", |
| 92 | + "pygments_lexer": "ipython3", |
| 93 | + "version": "3.11.4" |
| 94 | + } |
| 95 | + }, |
| 96 | + "nbformat": 4, |
| 97 | + "nbformat_minor": 5 |
| 98 | +} |
0 commit comments