Page 262 - AI Computer 10
P. 262
u By importing the values from a file (such as an Excel file), and then creating the DataFrame in Python
based on the values imported: In Jupyter Notebook, you can easily access the values from a file that is
stored on your computer. These values are used to create a dataframe. Now, let us learn how to create a
dataframe using imported values from a file.
The basic syntax is used to import an Excel file into Python in order to create a DataFrame is as follows:
import pandas as pd
data = pd.read_excel(r’File Location\File name.xlsx’)
df = pd.DataFrame(data, columns = [‘First Column Name’,’Second Column Name’,...])
print(df)
You should ensure that the column names specified in the code are similar to the column names mentioned in
the Excel file. Suppose a sheet made in Excel is stored in the F:\ drive of your computer in the following format:
Let us understand how to import data of Excel file in Python and assign it to the dataframe with the help of a
example given below:
import pandas as pd
df = pd.read_excel(r’F:\Worksheet_Cyber.xlsx’)
print(df)
The output of the above code is shown in the snapshot given below:
128
128