From d1e1fc5c9d637395e32235e45891698478929700 Mon Sep 17 00:00:00 2001 From: Krishna Kaushik <131583096+kRiShNa-429407@users.noreply.github.com> Date: Sat, 18 May 2024 22:37:38 +0530 Subject: [PATCH] Add files via upload --- .../Importing_and_Exporting_Data_in_Pandas.md | 291 ++++++++++++++++++ 1 file changed, 291 insertions(+) create mode 100644 contrib/pandas/Importing_and_Exporting_Data_in_Pandas.md diff --git a/contrib/pandas/Importing_and_Exporting_Data_in_Pandas.md b/contrib/pandas/Importing_and_Exporting_Data_in_Pandas.md new file mode 100644 index 0000000..a6949d2 --- /dev/null +++ b/contrib/pandas/Importing_and_Exporting_Data_in_Pandas.md @@ -0,0 +1,291 @@ +# Importing_and_Exporting_Data_in_Pandas + +>Created by Krishna Kaushik + +- **Now we're able to create `Series` and `DataFrames` in pandas, but we usually do not do this , in practice we import the data which is in the form of .csv (Comma Seperated Values) , a spreadsheet file or something similar.** + +- *Good news is that pandas allows for easy importing of data like this through functions such as ``pd.read_csv()`` and ``pd.read_excel()`` for Microsoft Excel files.* + +## 1. Importing from a Google sheet to a pandas dataframe + +*Let's say that you wanted to get the information from Google Sheet document into a pandas DataFrame.*. + +*You could export it as a .csv file and then import it using ``pd.read_csv()``.* + +*In this case, the exported .csv file is called `Titanic.csv`* + + +```python +## Importing Titanic Data set +import pandas as pd + +titanic_df= pd.read_csv("https://raw.githubusercontent.com/kRiShNa-429407/learn-python/main/contrib/pandas/Titanic.csv") +titanic_df +``` + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
pclasssurvivednamesexagesibspparchticketfarecabinembarkedboatbodyhome.dest
011Allen, Miss. Elisabeth Waltonfemale29.000024160211.3375B5S2NaNSt Louis, MO
111Allison, Master. Hudson Trevormale0.9212113781151.5500C22 C26S11NaNMontreal, PQ / Chesterville, ON
210Allison, Miss. Helen Lorainefemale2.0012113781151.5500C22 C26SNaNNaNMontreal, PQ / Chesterville, ON
310Allison, Mr. Hudson Joshua Creightonmale30.0012113781151.5500C22 C26SNaN135.0Montreal, PQ / Chesterville, ON
410Allison, Mrs. Hudson J C (Bessie Waldo Daniels)female25.0012113781151.5500C22 C26SNaNNaNMontreal, PQ / Chesterville, ON
.............................................
130430Zabour, Miss. Hilenifemale14.5010266514.4542NaNCNaN328.0NaN
130530Zabour, Miss. ThaminefemaleNaN10266514.4542NaNCNaNNaNNaN
130630Zakarian, Mr. Mapriededermale26.500026567.2250NaNCNaN304.0NaN
130730Zakarian, Mr. Ortinmale27.000026707.2250NaNCNaNNaNNaN
130830Zimmerman, Mr. Leomale29.00003150827.8750NaNSNaNNaNNaN
+

1309 rows × 14 columns

+
+ + + +The dataset I am using here for your reference is taken from the same repository i.e ``learn-python`` (https://raw.githubusercontent.com/kRiShNa-429407/learn-python/main/contrib/pandas/Titanic.csv) I uploaded it you can use it from there. + +**Now we've got the same data from the Google Spreadsheet , but now available as ``pandas DataFrame`` which means we can now apply all pandas functionality over it.** + +#### Note: The quiet important thing i am telling is that ``pd.read_csv()`` takes the location of the file (which is in your current working directory) or the hyperlink of the dataset from the other source. + +#### But if you want to import the data from Github you can't directly use its link , you have to first convert it to raw by clicking on the raw button present in the repo . + +#### Also you can't use the data directly from `Kaggle` you have to use ``kaggle API`` + +## 2. The Anatomy of DataFrame + +**Different functions use different labels for different things, and can get a little confusing.** + +- Rows are refer as ``axis=0`` +- columns are refer as ``axis=1`` + +## 3. Exporting Data + +**OK, so after you've made a few changes to your data, you might want to export it and save it so someone else can access the changes.** + +**pandas allows you to export ``DataFrame's`` to ``.csv`` format using ``.to_csv()``, or to a spreadsheet format using .to_excel().** + +### Exporting a dataframe to a CSV + +**We haven't made any changes yet to the ``titanic_df`` DataFrame but let's try to export it.** + + +```python +#Export the titanic_df DataFrame to csv +titanic_df.to_csv("exported_titanic.csv") +``` + +Running this will save a file called ``exported_titanic.csv`` to the current folder.