Skip to content

Commit 2016706

Browse files
authored
Merge pull request larymak#3 from Mahmoud-alzoubi95/omar
Write using pandas
2 parents aeae507 + c486421 commit 2016706

File tree

4 files changed

+153
-3
lines changed

4 files changed

+153
-3
lines changed

CSV_files/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,7 @@
1212
- using csv.DictWritter class
1313

1414
* **Write using cvs.DictWritter() class**: this class returns a writer object which maps directories onto output rows.
15+
16+
17+
* **Write using pandas module**:You can save your Pandas DataFrame as a CSV file with .to_csv(data)
1518

CSV_files/poetry.lock

Lines changed: 120 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CSV_files/pyproject.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[tool.poetry]
2+
name = "csv_files"
3+
version = "0.1.0"
4+
description = ""
5+
authors = ["Omar-zoubi <o.fandi02@gmail.com>"]
6+
7+
[tool.poetry.dependencies]
8+
python = "^3.9"
9+
pandas = "^1.3.0"
10+
11+
[tool.poetry.dev-dependencies]
12+
13+
[build-system]
14+
requires = ["poetry-core>=1.0.0"]
15+
build-backend = "poetry.core.masonry.api"

CSV_files/write_csv.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import csv
2+
import pandas as pd
3+
4+
25

36

47
def write_to_csv_files_using_DictWriter_class(data,fields,filename):
@@ -13,7 +16,9 @@ def write_to_csv_files_using_DictWriter_class(data,fields,filename):
1316
# writing data rows
1417
writer.writerows(data)
1518

16-
19+
def write_by_pandas(name_dict):
20+
df = pd.DataFrame(name_dict)
21+
return df
1722

1823
if __name__=="__main__":
1924
# my data rows as dictionary objects
@@ -27,7 +32,14 @@ def write_to_csv_files_using_DictWriter_class(data,fields,filename):
2732
fields = ['name','course']
2833

2934
# name of csv file
30-
filename = "CSV_files/assets/course_name.csv"
35+
filename = "assets/course_name.csv"
3136

3237
# writing to csv file
33-
print(write_to_csv_files_using_DictWriter_class(mydata,fields,filename))
38+
print(write_to_csv_files_using_DictWriter_class(mydata,fields,filename))
39+
40+
41+
name_dict = {
42+
'Name': ['Omar','Mahmoud','Noura','Raneem'],
43+
'Score': [82,86,84,65]
44+
}
45+
print(write_by_pandas(name_dict))

0 commit comments

Comments
 (0)