Skip to main content

Importing the Dataset in Python

    That is how to import data from csv file in Python.
import pandas as pd
dataset = pd.read_csv('Data.csv')
    It will create a data frame and it will contain exactly the same rows and columns and values from file Data.csv. We will use library pandas which we gave it the shortcut named pd.
x = dataset.iloc[:, :-1].values
y = dataset.iloc[:, -1].values
    We call x for the matrix of features and y for the dependent variable vector. First we will get data from dataset so I am adding a dot after dataset to use pandas's data frame "iloc". We can specify the rows that we want to get and put into x. And the trick to take all the rows whatever dataset you have with whatever number of rows is to add a ":". Because a colon in python means a range and when we specify a range without the lower bound and neither the upper bound that means in Python that we are taking everything in the range.
    We have to specify which columns want to select with the indexes and to separate he rows that we just took from the columns we need here to add a comma. Add a new range here which this time will be colon minus one. Because colon here means the range. We are taking a range here on the left we have nothing that means that we are taking the first index in the index 0 because index in Python started from 0 and then we are going up to minus one. Because the last column in Python means the index of the last column. It will take all the columns excluding the last one.
    In order to finish this line of code, we add dot values and this means that we are taking indeed all the values in all the rows of this dataset and all columns except the last column.
    In y, we only want to get the last column. We definitely don't want to get a range. Minus one is exactly the index of the last column.
The last you can check the data which you import from Data.csv to x and y by
print(x)
print(y)

Comments

Popular Posts

7554 bất ngờ mở miễn phí tặng game thủ nhân dịp kỷ niệm 65 năm chiến thắng Điện Biên Phủ

  "Tiến tới kỷ niệm 65 năm chiến thắng Điện Biên Phủ(7/5/1954 - 7/5/2019) . Và cũng nhân dịp kỷ niệm 10 năm làm game của Hiker Games. Như đã hứa, xin được gửi tặng cộng đồng game thủ Việt Nam phiên bản 7554 hoàn toàn miễn phí. Các bạn có thể truy cập vào website http://www.7554.vn để download. Để đảm bảo không có hiểu nhầm nào, xin lưu ý, đây không phải là phiên bản làm lại của 7554.  Đây chỉ là phiên bản đã loại bỏ phần kiểm tra bản quyền để bất cứ ai cũng có thể tải về và trải nghiệm game. Chúc các bạn chơi game vui vẻ!"  Via 7554 Dưới đây là video hướng dẫn tải game:  Chúc các bạn chơi game vui vẻ !

Nha's Blog #20260407 A Long Tuesday of Paperwork, Rain

A Long Tuesday of Paperwork, Rain Một ngày thứ Ba dài với giấy tờ, mưa Today was Tuesday, normally a working day, but I took a day off to handle paperwork for my permanent residency. The morning was gloomy, and the streets were still wet from days of spring rain. Thinking I had the whole day, I went to pachinko early since machines after reset are often better. Things went smoothly, and I managed to win a little. Hôm nay là thứ Ba, lẽ ra là ngày làm việc nhưng tôi xin nghỉ để đi làm giấy tờ xin vĩnh trú. Buổi sáng khá âm u, đường vẫn còn ướt do mưa xuân mấy ngày nay. Nghĩ rằng mình có cả ngày nên tôi tranh thủ đi pachinko từ sớm vì máy sau khi reset thường dễ chơi hơn. Mọi chuyện khá suôn sẻ và tôi thắng được một chút. Around 10 AM, I headed to the station to start my paperwork journey, only to realize I had forgotten my bank book and insurance documents. I had to turn back. On the way home, it started raining lightly, but that turned out to be a good thing since I could grab an umbrel...

Nha's Blog #20260411 A Conan Movie Night and a Surprisingly Endless Hotpot

A Conan Movie Night and a Surprisingly Endless Hotpot Một buổi xem Conan và nồi lẩu “ăn mãi không hết” Today is Saturday, and as a fan of Detective Conan, I couldn’t miss the latest movie release. I had booked my ticket a few days earlier, and as soon as it officially premiered, I headed straight to the cinema. I arrived about 15 minutes late due to buying popcorn and drinks, but that was intentional since movies usually start later after all the ads and warnings. Hôm nay là thứ Bảy, và là một fan của Thám tử lừng danh Conan nên tôi không thể bỏ qua phần phim mới nhất chiếu rạp. Tôi đã đặt vé từ vài ngày trước, và ngay khi phim chính thức công chiếu, tôi lập tức ra rạp. Tôi vào muộn khoảng 15 phút do mua bỏng và nước, nhưng đó là trong tính toán vì phim thường chiếu muộn sau hàng loạt quảng cáo. About five minutes after I sat down, the movie started. I won’t spoil anything, but I’ll share my thoughts. The title hints at a case happening on the road, and interestingly, the setting s...