In real world you will be working with different types and sources of data, hence you must learn how to load this into your DataFrame. We will maintain the same example from the previous post, but from different sources:
● CSV Files
For CSV files, load data using the following command:
df = pd.read_csv('sales.csv')
df
You will have the following output:
Remember that CSV files do not index files the way DataFrames do. Therefore, you will have to use the index_col designation to read the files as shown below:
df = pd.read_csv('sales.csv', index_col=0)
df
You will have the following output:
From the example above, the index is set to column zero. However, you will notice that when using CSV files, most of them lack an index column. For this reason, you can easily skip this step without any repercussions.
● JSON Files
JSON files are compatible with Python, so reading them should be easy as follows:
df = pd.read_json('sales.json')
df
You will have the following output:
In this case, the index is correct because Pandas uses the JSON indices.
● CSV Files
For CSV files, load data using the following command:
df = pd.read_csv('sales.csv')
df
You will have the following output:
Remember that CSV files do not index files the way DataFrames do. Therefore, you will have to use the index_col designation to read the files as shown below:
df = pd.read_csv('sales.csv', index_col=0)
df
You will have the following output:
From the example above, the index is set to column zero. However, you will notice that when using CSV files, most of them lack an index column. For this reason, you can easily skip this step without any repercussions.
● JSON Files
JSON files are compatible with Python, so reading them should be easy as follows:
df = pd.read_json('sales.json')
df
You will have the following output:
In this case, the index is correct because Pandas uses the JSON indices.
0 comments:
Post a Comment