Reading/Writing data (CSV, Excel, SQL)
Reading/Writing Data (CSV, Excel, SQL) What is it? Data manipulation is the process of preparing raw data for analysis and drawing meaningful insights fr...
Reading/Writing Data (CSV, Excel, SQL) What is it? Data manipulation is the process of preparing raw data for analysis and drawing meaningful insights fr...
What is it?
Data manipulation is the process of preparing raw data for analysis and drawing meaningful insights from it. This process involves reading data from different sources, cleaning and transforming it, and finally, writing it back to a new format for further analysis.
Common formats:
CSV (Comma-Separated Values): This is a widely used format where data is separated by commas and enclosed in double quotes.
Excel: A proprietary format developed by Microsoft with a powerful data analysis tool named Excel.
SQL (Structured Query Language): This is a powerful query language used for data manipulation, analysis, and retrieval in various databases.
Reading data:
Python for Business Analytics: We will primarily focus on reading data from CSV and Excel formats using Python's powerful pandas library.
Reading CSV:
python
import pandas as pd
data = pd.read_csv("data.csv")
name = data["Name"]
age = data["Age"]
print(data.head())
Reading Excel:
python
import pandas as pd
data = pd.read_excel("data.xlsx")
name = data["Name"]
age = data["Age"]
print(data.head())
Writing data:
Once we have prepared our data, we can write it back to various formats.
CSV:
python
data.to_csv("data_output.csv", index=False)
Excel:
python
data.to_excel("data_output.xlsx", index=False)
SQL:
python
connection = sqlite3.connect("my_database.db")
cursor = connection.cursor()
cursor.execute("INSERT INTO table_name (column1, column2) VALUES (?, ?)", ("data1", "data2"))
connection.commit()
cursor.close()
Key takeaways:
Data manipulation involves reading, cleaning, and writing data from different sources.
Python's pandas library is widely used for reading and writing data in various formats.
Reading data from CSV and Excel involves using specific functions, and writing data to these formats can be achieved with similar methods