{ "cells": [ { "cell_type": "code", "execution_count": 7, "id": "1a33d2cd-5d9f-40a6-bc28-5b2e8026226c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ABC
0147
\n", "
" ], "text/plain": [ " A B C\n", "0 1 4 7" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "\n", "# Example DataFrame\n", "data = {\n", " 'A': [1, 2, 3],\n", " 'B': [4, 5, 6],\n", " 'C': [7, 8, 9]\n", "}\n", "df = pd.DataFrame(data)\n", "\n", "# Create a new DataFrame with only the first row using .iloc[0:1]\n", "first_row_df = df.iloc[0:1]\n", "first_row_df" ] }, { "cell_type": "code", "execution_count": 6, "id": "51c38582-bccf-4e03-918f-9d65bbec1dda", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "New DataFrame with First Row:\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ABC
0147
\n", "
" ], "text/plain": [ " A B C\n", "0 1 4 7" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Create a new DataFrame with only the first row using .head(1)\n", "first_row_df = df.head(1)\n", "\n", "print(\"New DataFrame with First Row:\")\n", "first_row_df" ] }, { "cell_type": "code", "execution_count": null, "id": "664e8423-f560-4c23-8a94-242f8ec283cd", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.3" } }, "nbformat": 4, "nbformat_minor": 5 }