Skip to content

Commit b7e1773

Browse files
committed
Reading/ writing file and pandas
1 parent 765772c commit b7e1773

File tree

7 files changed

+313
-0
lines changed

7 files changed

+313
-0
lines changed

class.ipynb

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "32dcc4c5",
6+
"metadata": {},
7+
"source": [
8+
"Class"
9+
]
10+
},
11+
{
12+
"cell_type": "markdown",
13+
"id": "48b72d0b",
14+
"metadata": {},
15+
"source": [
16+
"A class is like an object constructor or a blueprint for creating objects"
17+
]
18+
},
19+
{
20+
"cell_type": "code",
21+
"execution_count": 1,
22+
"id": "f66cb092",
23+
"metadata": {},
24+
"outputs": [
25+
{
26+
"name": "stdout",
27+
"output_type": "stream",
28+
"text": [
29+
"10\n"
30+
]
31+
}
32+
],
33+
"source": [
34+
"class myclass:\n",
35+
" x =10;\n",
36+
"p1 =myclass();\n",
37+
"print(p1.x)"
38+
]
39+
},
40+
{
41+
"cell_type": "code",
42+
"execution_count": 8,
43+
"id": "43a52efd",
44+
"metadata": {},
45+
"outputs": [
46+
{
47+
"name": "stdout",
48+
"output_type": "stream",
49+
"text": [
50+
"red\n",
51+
"31.400000000000002\n"
52+
]
53+
}
54+
],
55+
"source": [
56+
"import matplotlib.pyplot as plt\n",
57+
"\n",
58+
"class Circle:\n",
59+
" def __init__(self, r, c):\n",
60+
" self.radius = r\n",
61+
" self.color = c\n",
62+
"\n",
63+
" def add(self, r):\n",
64+
" self.radius = r\n",
65+
" return 2 * 3.14 * r\n",
66+
"\n",
67+
" \n",
68+
"\n",
69+
"c1 = Circle(10, 'red')\n",
70+
"print(c1.color)\n",
71+
"p = c1.add(5)\n",
72+
"print(p)\n",
73+
"\n",
74+
"\n"
75+
]
76+
},
77+
{
78+
"cell_type": "code",
79+
"execution_count": null,
80+
"id": "21862d57",
81+
"metadata": {},
82+
"outputs": [],
83+
"source": []
84+
}
85+
],
86+
"metadata": {
87+
"kernelspec": {
88+
"display_name": "Python 3",
89+
"language": "python",
90+
"name": "python3"
91+
},
92+
"language_info": {
93+
"codemirror_mode": {
94+
"name": "ipython",
95+
"version": 3
96+
},
97+
"file_extension": ".py",
98+
"mimetype": "text/x-python",
99+
"name": "python",
100+
"nbconvert_exporter": "python",
101+
"pygments_lexer": "ipython3",
102+
"version": "3.13.2"
103+
}
104+
},
105+
"nbformat": 4,
106+
"nbformat_minor": 5
107+
}

file.ipynb

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "d224a3d2",
6+
"metadata": {},
7+
"source": [
8+
"##Reading File with Open"
9+
]
10+
},
11+
{
12+
"cell_type": "code",
13+
"execution_count": 32,
14+
"id": "d8d31f0f",
15+
"metadata": {},
16+
"outputs": [
17+
{
18+
"name": "stdout",
19+
"output_type": "stream",
20+
"text": [
21+
"Hello My n\n"
22+
]
23+
}
24+
],
25+
"source": [
26+
"with open(\"helllo.txt\",\"r\") as f2: # use of with statement is better practice to open a file becuase it automatically closes the file\n",
27+
" # filest = f2.read();\n",
28+
" # print(filest);\n",
29+
" # print(f2.closed);\n",
30+
" # print(filest)\n",
31+
" # line = f2.readline() # read first line \n",
32+
" # line2 = f2.readline() # read second line it called by sequence\n",
33+
" # for line in f2: # each line over loop\n",
34+
" # print(line)\n",
35+
" specialline = f2.readline(10) # till number of character\n",
36+
" print(specialline)\n",
37+
" \n",
38+
"\n",
39+
"\n",
40+
"\n",
41+
"# f1 = open(\"helllo.txt\",\"r\"); # to normal open the file \n",
42+
"# ff = f1.read();\n",
43+
"# print(ff)\n",
44+
"# print(f1.closed);\n",
45+
"# print(filest)\n"
46+
]
47+
},
48+
{
49+
"cell_type": "code",
50+
"execution_count": null,
51+
"id": "46e419e2",
52+
"metadata": {},
53+
"outputs": [],
54+
"source": [
55+
"with open(\"write.txt\",\"w\") as f3: # Created a file or we can write on existing file but it will remove all previous thing on file\n",
56+
" f3.write(\"AI is dangerous:::::\\n\")\n",
57+
" f3.write(\"Yellow\\n\")\n",
58+
"\n",
59+
"with open(\"write.txt\",\"a\") as f4: # it will not create a new file but add lines into existing file without removing previous text.\n",
60+
" f4.write(\"this will go to next line \") \n",
61+
"\n",
62+
"\n",
63+
"##Write data of one file into other file \n",
64+
"with open(\"helllo.txt\",\"r\") as readfile:\n",
65+
" with open(\"newfile\",\"w\")as writefile:\n",
66+
" for line in readfile:\n",
67+
" writefile.write(line)"
68+
]
69+
},
70+
{
71+
"cell_type": "code",
72+
"execution_count": null,
73+
"id": "92f9de46",
74+
"metadata": {},
75+
"outputs": [],
76+
"source": []
77+
}
78+
],
79+
"metadata": {
80+
"kernelspec": {
81+
"display_name": "Python 3",
82+
"language": "python",
83+
"name": "python3"
84+
},
85+
"language_info": {
86+
"codemirror_mode": {
87+
"name": "ipython",
88+
"version": 3
89+
},
90+
"file_extension": ".py",
91+
"mimetype": "text/x-python",
92+
"name": "python",
93+
"nbconvert_exporter": "python",
94+
"pygments_lexer": "ipython3",
95+
"version": "3.13.2"
96+
}
97+
},
98+
"nbformat": 4,
99+
"nbformat_minor": 5
100+
}

file1.xlsx

29.6 KB
Binary file not shown.

helllo.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
AI is dangerous:::::

newfile

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
AI is dangerous:::::

pandas.ipynb

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "4c3a8bad",
6+
"metadata": {},
7+
"source": [
8+
"##Pandas :: Loading"
9+
]
10+
},
11+
{
12+
"cell_type": "markdown",
13+
"id": "0f83db65",
14+
"metadata": {},
15+
"source": [
16+
"Pandas is a popular library for data analysis.\n",
17+
"we can import pandas by using import command \n",
18+
"import pandas --"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": 27,
24+
"id": "07e0d62b",
25+
"metadata": {},
26+
"outputs": [
27+
{
28+
"name": "stdout",
29+
"output_type": "stream",
30+
"text": [
31+
" Album Year\n",
32+
"0 Believer 2018\n",
33+
"1 Masti 2020\n",
34+
"2 Fade way 2009\n",
35+
" Album\n",
36+
"0 Believer\n",
37+
"1 Masti\n",
38+
"2 Fade way\n",
39+
"P-00089\n",
40+
"MARMUL FALCON TRADE & Co\n",
41+
"Believer\n"
42+
]
43+
}
44+
],
45+
"source": [
46+
"import pandas as pd;\n",
47+
"\n",
48+
"df = pd.read_excel('file1.xlsx'); # using pandas method read_excel to read excel files\n",
49+
"x = df[['Customer']] #pick one column \n",
50+
"\n",
51+
"# print(df.head());\n",
52+
"\n",
53+
"album = {\n",
54+
" 'Album':['Believer','Masti','Fade way'],\n",
55+
" 'Year':['2018','2020','2009']\n",
56+
"\n",
57+
"}\n",
58+
"songs_info = pd.DataFrame(album)\n",
59+
"print(songs_info)\n",
60+
"y = songs_info[['Album']]; # take one or multiple column to make a different dataframe from an existing dataFrame or file\n",
61+
"print(y)\n",
62+
"\n",
63+
"#iloc[r,c] method to get value from specific row or column r - row number c is column number;\n",
64+
"print(df.iloc[0,1])\n",
65+
"print(df.loc[3,'Customer'])\n",
66+
"songs_infos = songs_info\n",
67+
"songs_infos.index=[\"a\",\"b\",\"c\"] # changing index of a dataframe \n",
68+
"print(songs_infos.loc['a','Album'])"
69+
]
70+
},
71+
{
72+
"cell_type": "code",
73+
"execution_count": null,
74+
"id": "83438a76",
75+
"metadata": {},
76+
"outputs": [],
77+
"source": []
78+
}
79+
],
80+
"metadata": {
81+
"kernelspec": {
82+
"display_name": "Python 3",
83+
"language": "python",
84+
"name": "python3"
85+
},
86+
"language_info": {
87+
"codemirror_mode": {
88+
"name": "ipython",
89+
"version": 3
90+
},
91+
"file_extension": ".py",
92+
"mimetype": "text/x-python",
93+
"name": "python",
94+
"nbconvert_exporter": "python",
95+
"pygments_lexer": "ipython3",
96+
"version": "3.13.2"
97+
}
98+
},
99+
"nbformat": 4,
100+
"nbformat_minor": 5
101+
}

write.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
AI is dangerous:::::
2+
Yellow
3+
this will go to next line

0 commit comments

Comments
 (0)