Test - 1
Test - 1
}, { "cell_type": "code", "execution_count": 6, "id": "9cd4e75f-3f27-4cde-ba22-f5f5c042a1d2", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream",
"text": [ "Empty DataFrame\n", "Columns: []\n", "Index: []\n" ] } ], "source": [ "df = pd.DataFrame()\n", "print(df)" ] }, { "cell_type": "code", "execution_count": 14, "id": "7360f1ba-
21df-4309-b98b-d914f37de61e", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
0
03
14
25
\n", "
" ], "text/plain": [ " 0\n", "0 3\n", "1 4\n", "2 5" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ser = pd.Series([3,4, 5])\n", "df =
pd.DataFrame(data=ser)\n", "df" ] }, { "cell_type": "code", "execution_count": 18, "id": "900e443e-f994-476c-b7bf-c2ecc02c319e", "metadata": { "tags": [] }, "outputs": [ { "data": {
"text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
0
01
12
23
34
45
\n", "
" ], "text/plain": [ " 0\n", "0 1\n", "1 2\n", "2 3\n", "3 4\n", "4 5" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#create a DataFrame from
list=arry\n", "f =[1,2,3,4,5]\n", "df= pd.DataFrame(f)\n", "df" ] }, { "cell_type": "code", "execution_count": 4, "id": "ae438e57-0d11-46b3-9715-a9c187204e5b", "metadata": { "tags":
[] }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Name Age
0a 20
1b 30
2e 50
\n", "
" ], "text/plain": [ " Name Age\n", "0 a 20\n", "1 b 30\n", "2 e 50" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "data =[['a', 20],['b
',30],['e',50] ]\n", "#df = pd.DataFrame(data,columns=['Name', 'Age'])\n", "#df = pd.DataFrame(data,columns=['Name', 'Age'])\n", "#df\n", "# when i want to chang outpout the
number float ,intger ,double \n", "df =pd.DataFrame(data,columns=['Name', 'Age'] )\n", "df\n" ] }, { "cell_type": "code", "execution_count": 42, "id": "e82601e1-5d50-43db-8679-
e3eead8e4820", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Name Age
0 tom 25
1 jeck 33
2 steve 55
3 Ricky 32
\n", "
" ], "text/plain": [ " Name Age\n", "0 tom 25\n", "1 jeck 33\n", "2 steve 55\n", "3 Ricky 32" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [
"#Dictionaries\n", "data ={'Name':['tom', 'jeck', 'steve', 'Ricky'], 'Age':[25,33,55,32]}\n", "df = pd.DataFrame(data)\n", "df" ] }, { "cell_type": "code", "execution_count": 43, "id":
"6182f5b3-9e99-46ef-b70a-3d88740163b4", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Name Age
rank1 tom 25
rank2 jeck 33
rank3 steve 55
rank4 Ricky 32
\n", "
" ], "text/plain": [ " Name Age\n", "rank1 tom 25\n", "rank2 jeck 33\n", "rank3 steve 55\n", "rank4 Ricky 32" ] }, "execution_count": 43, "metadata": {}, "output_type":
"execute_result" } ], "source": [ "#how to change the index 0,1,2,3 that set\n", "data ={'Name':['tom', 'jeck', 'steve', 'Ricky'], 'Age':[25,33,55,32]}\n", "df = pd.DataFrame(data,
index=['rank1', 'rank2', 'rank3', 'rank4',])\n", "df\n" ] }, { "cell_type": "code", "execution_count": 13, "id": "d5c2a475-ac12-46dd-a641-38ab11e591db", "metadata": { "tags": [] },
"outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Name Age
0 tom 25
1 jeck 33
2 steve 55
3 Ricky 32
\n", "
" ], "text/plain": [ " Name Age\n", "0 tom 25\n", "1 jeck 33\n", "2 steve 55\n", "3 Ricky 32" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [
"#dict columns \n", "import pandas as pd\n", "data ={'Name':['tom', 'jeck', 'steve', 'Ricky'], 'Age':[25,33,55,32]}\n", "df = pd.DataFrame.from_dict(data, orient='columns')\n", "df" ]
}, { "cell_type": "code", "execution_count": 106, "id": "62af75a6-5a02-4df9-a31e-18cc606a469a", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
a b c f
Name tom jeck steve Ricky
Age 25 33 55 32
\n", "
" ], "text/plain": [ " a b c f\n", "Name tom jeck steve Ricky\n", "Age 25 33 55 32" ] }, "execution_count": 106, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#name
the index the the value \n", "data ={'Name':['tom', 'jeck', 'steve', 'Ricky'], 'Age':[25,33,55,32]}\n", "df = pd.DataFrame.from_dict(data, orient='index',columns=['a','b','c','f'])\n", "df" ]
}, { "cell_type": "code", "execution_count": 98, "id": "7822175c-5cbd-4a7e-8568-a0c6c67f0f77", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
a b c d
Name tom jeck steve Ricky
Age 25 33 55 32
\n", "
" ], "text/plain": [ " a b c d\n", "Name tom jeck steve Ricky\n", "Age 25 33 55 32" ] }, "execution_count": 98, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#ues
Used together \n", "data ={'Name':['tom', 'jeck', 'steve', 'Ricky'], 'Age':[25,33,55,32]}\n", "df = pd.DataFrame.from_dict(data, orient='index', columns=['a','b','c','d',])\n", "df" ] }, {
"cell_type": "code", "execution_count": 14, "id": "e2f08f58-7ebe-40c2-a01d-a2627c5d8ef8", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream",
"text": [ "0 NaN\n", "1 NaN\n", "Name: Name, dtype: float64\n" ] } ], "source": [ "import pandas as pd\n", "\n", "data =[{'a': 2 , 'b': 33 , }, {'a': 22 ,'b':33 , 'c':33}]\n", "df =
pd.DataFrame(data,columns=['Name', 'Age'])\n", "#we can also identyfil the columns using [dc]\n", "#print(dc[1])\n", "print(df['Name']د\n", " " ] }, { "cell_type": "code",
"execution_count": 19, "id": "775e6cd4-c634-4bdb-b5cf-5ff763202a3f", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ab c
0 1 2 NaN
1 5 5 20.0
\n", "
" ], "text/plain": [ " a b c\n", "0 1 2 NaN\n", "1 5 5 20.0" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# when dict dont have value you
can see the \"c\"\n", "import pandas as pd \n", "data = [{'a':1, 'b':2},{'a':5, 'b':5, 'c':20}]\n", "df=pd.DataFrame(data)\n", "df " ] }, { "cell_type": "code", "execution_count": 20, "id":
"47b18ecd-52ad-42bd-b617-e724c797d78b", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ab c
first 1 2 NaN
decond 5 5 20.0
\n", "
" ], "text/plain": [ " a b c\n", "first 1 2 NaN\n", "decond 5 5 20.0" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd \n",
"data = [{'a':1, 'b':2},{'a':5, 'b':5, 'c':20}]\n", "df=pd.DataFrame(data, index=['first', 'decond'])\n", "df " ] }, { "cell_type": "code", "execution_count": 27, "id": "bedba0e3-029e-4e81-
b5bf-a9a1669eeda8", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 0 1\n", "0 Alex 10\n", "1 bod 12\n", "2 clarke 13\n", "-------------\n",
"0 Alex\n", "1 bod\n", "2 clarke\n", "Name: 0, dtype: object\n" ] } ], "source": [ "#column Selection\n", "import pandas as pd \n", "data = [['Alex',10],['bod',12],['clarke',13]]\n", "df =
pd.DataFrame(data)\n", "print(df)\n", "print(\"-------------\")\n", "print (df[0])" ] }, { "cell_type": "code", "execution_count": 30, "id": "bd899795-8b40-43e6-8978-ba728d144911",
"metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " name age\n", "0 Alex 10\n", "1 bod 12\n", "2 clarke 13\n", "-------------\n", "0 10\n", "1
12\n", "2 13\n", "Name: age, dtype: int64\n" ] } ], "source": [ "#column Selection\n", "import pandas as pd \n", "data = [['Alex',10],['bod',12],['clarke',13]]\n", "df =
pd.DataFrame(data,columns=['name','age'])\n", "print(df)\n", "print(\"-------------\")\n", "print (df['age'])" ] }, { "cell_type": "code", "execution_count": 31, "id": "b48233c2-7310-4ce2-
aa52-c445c46abdfc", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
name age address
0 Alex 10 aaa
1 bod 12 gfg
2 clarke 13 dde
\n", "
" ], "text/plain": [ " name age address\n", "0 Alex 10 aaa\n", "1 bod 12 gfg\n", "2 clarke 13 dde" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ],
"source": [ "#columns Addition\n", "df['address']=['aaa','gfg','dde']\n", "df\n" ] }, { "cell_type": "code", "execution_count": 32, "id": "59601cc5-dfa2-417a-b936-7074241c2f9f",
"metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "deleting the fires colum using del \n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
name address
0 Alex aaa
1 bod gfg
2 clarke dde
\n", "
" ], "text/plain": [ " name address\n", "0 Alex aaa\n", "1 bod gfg\n", "2 clarke dde" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#pop=
whem i wnat deleting ues the pop \n", "print(\"deleting the fires colum using del \")\n", "df.pop('age')\n", "df" ] }, { "cell_type": "code", "execution_count": 35, "id": "7c6dcea1-8f95-
4749-91c9-f35ba0c93256", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
one tow
a 1.0 1
b 2.0 2
c 3.0 3
d NaN 4
\n", "
" ], "text/plain": [ " one tow\n", "a 1.0 1\n", "b 2.0 2\n", "c 3.0 3\n", "d NaN 4" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Selection
by label \n", "#Rows can be selected by passing row label to a loc function\n", "d = {'one': pd.Series([1,2,3,], index=['a', 'b', 'c']),\n", " 'tow': pd.Series([1,2,3,4], index= ['a', 'b' ,'c',
'd'])}\n", "df = pd.DataFrame(d)\n", "df" ] }, { "cell_type": "code", "execution_count": 37, "id": "11b18918-d561-4628-99aa-eb3715f7bb60", "metadata": { "tags": [] }, "outputs": [ {
"data": { "text/plain": [ "one 1.0\n", "tow 1.0\n", "Name: a, dtype: float64" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Selection by
label \n", "#Rows can be selected by passing row label to a loc function\n", "d = {'one': pd.Series([1,2,3,], index=['a', 'b', 'c']),\n", " 'tow': pd.Series([1,2,3,4], index= ['a', 'b' ,'c',
'd'])}\n", "df = pd.DataFrame(d)\n", "#df\n", "df.loc['a']" ] }, { "cell_type": "code", "execution_count": 4, "id": "295b96fd-33d4-4879-b7a0-21e57d1d14ed", "metadata": { "tags": [] },
"outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ab
012
134
056
178
\n", "
" ], "text/plain": [ " a b\n", "0 1 2\n", "1 3 4\n", "0 5 6\n", "1 7 8" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Additiom of rows \n",
"import pandas as pd\n", "df = pd.DataFrame ([[1,2],[3,4]], columns =['a','b'])\n", "df2= pd.DataFrame ([[5,6], [7,8]], columns =['a','b'])\n", "\n", "df = df._append(df2)\n", "df " ] }, {
"cell_type": "code", "execution_count": 41, "id": "94211b94-1fee-44aa-8014-e3c856d20870", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
" \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
A B C D E F
0 1.0 2013-01-02 1.0 3 test foo
1 1.0 2013-01-02 1.0 3 train foo
2 1.0 2013-01-02 1.0 3 test foo
3 1.0 2013-01-02 1.0 3 train foo
\n", "
" ], "text/plain": [ " A B C D E F\n", "0 1.0 2013-01-02 1.0 3 test foo\n", "1 1.0 2013-01-02 1.0 3 train foo\n", "2 1.0 2013-01-02 1.0 3 test foo\n", "3 1.0 2013-01-02 1.0 3 train foo" ]
}, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df2 = pd.DataFrame(\n", " {\n", " \"A\": 1.0,\n", " \"B\": pd.Timestamp(\"20130102\"),\n", "
\"C\": pd.Series(1, index=list(range(4)), dtype=\"float32\"),\n", " \"D\": np.array([3] * 4, dtype=\"int32\"),\n", " \"E\": pd.Categorical([\"test\", \"train\", \"test\", \"train\"]),\n", " \"F\":
\"foo\",\n", " }\n", " )\n", "df2" ] }, { "cell_type": "code", "execution_count": 39, "id": "8156ef63-3fa5-4248-bc11-272e2e09391d", "metadata": { "tags": [] }, "outputs": [ { "data": {
"text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
" \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
A B C D
2013-01-01 -0.758646 -0.831293 0.213619 -1.012936
2013-01-02 0.858895 -0.115271 1.743286 -0.897626
2013-01-03 0.848148 1.458501 -1.321339 0.807906
2013-01-04 -0.687208 1.274620 -0.273903 1.923105
A B C D
2013-01-05 -1.304873 -1.154423 0.531805 -0.959242