熊貓數據集_大熊貓數據框的5個基本操作

熊貓數據集

Tips and Tricks for Data Science

數據科學技巧與竅門

Pandas is a powerful and easy-to-use software library written in the Python programming language, and is used for data manipulation and analysis.

Pandas是使用Python編程語言編寫的功能強大且易于使用的軟件庫,可用于數據處理和分析。

Installing pandas: https://pypi.org/project/pandas/

安裝熊貓: https : //pypi.org/project/pandas/

pip install pandas

pip install pandas

什么是Pandas DataFrame? (What is a Pandas DataFrame?)

A pandas DataFrame is a two dimensional data structure which stores data in a tabular form. Every row and column are labeled and can hold data of any type.

pandas DataFrame是二維數據結構,以表格形式存儲數據。 每行和每列都有標簽,可以保存任何類型的數據。

Here is an example:

這是一個例子:

Image for post
First 3 rows of the Titanic: Machine Learning from Disaster dataset
泰坦尼克號的前三行:災難數據中的機器學習

1.創建一個熊貓DataFrame (1. Creating a pandas DataFrame)

The pandas.DataFrame constructor:

pandas.DataFrame構造函數:

pandas.DataFrame(data=None, index=None, columns=None, dtype=None, copy=False

pandas.DataFrame(data=None, index=None, columns=None, dtype=None, copy=False

data This parameter serves as the input to make a DataFrame, which could be a NumPy ndarray, iterable, dict or another DataFrame. An ndarray is a multidimensional container of items of the same type and size. An iterable is any Python object capable of returning its members one at a time, permitting to be iterated over in a for-loop. Some examples for iterables are lists, tuples and sets. Dict here can refer to pandas Series, arrays, constants or list-like objects.

data此參數用作制作DataFrame的輸入,該DataFrame可以是NumPy ndarray,可迭代,dict另一個DataFramendarray是具有相同類型和大小的項目的多維容器。 可迭代對象是能夠一次返回其成員并允許在for循環中對其進行迭代的任何Python對象。 可迭代的一些示例是列表,元組和集合。 這里的Dict可以引用pandas系列,數組,常量或類似列表的對象。

indexThis parameter could have an Index or an array-like data type and serves as the index for the row labels in the resulting DataFrame. If no indexing information is provided, this parameter will default to RangeIndex.

index此參數可以具有Index或類似數組的數據類型,并用作結果DataFrame中行標簽的索引。 如果沒有提供索引信息,則此參數將默認為RangeIndex 。

columnsThis parameter could have an Index or an array-like data type and serves as the index for the column labels in the resulting DataFrame. If no indexing information is provided, this parameter will default to RangeIndex.

columns此參數可以具有Index或類似數組的數據類型,并用作結果DataFrame中列標簽的索引。 如果沒有提供索引信息,則此參數將默認為RangeIndex 。

dtypeEach column in the DataFrame can only have a single data type. This parameter is used to force a certain data type. By default, datatype is inferred from data.

DTYPE在數據幀的每一列只能有一種數據類型。 此參數用于強制某種數據類型。 默認情況下,從數據推斷出數據類型。

copyWhen this parameter is set to True, and the input data is a DataFrame or a 2D ndarray, data is copied into the resulting DataFrame. By default, copy is set to False.

復制如果將此參數設置為True,并且輸入數據是DataFrame或2D ndarray,則將數據復制到結果DataFrame中。 默認情況下,復制設置為False。

從Python字典創建Pandas DataFrame (Creating a Pandas DataFrame from a Python Dictionary)

import pandas as pd

import pandas as pd

d = {'Name' : ['John', 'Adam', 'Jane'], 'Age' : [25, 18, 30]}pd.DataFrame(d)

d = {'Name' : ['John', 'Adam', 'Jane'], 'Age' : [25, 18, 30]}pd.DataFrame(d)

Image for post

The index parameter can be used to change the default row index and the columns parameter can be used to change the order of the keys:

index參數可用于更改默認行索引, columns參數可用于更改鍵的順序:

d = {'Name' : ['John', 'Adam', 'Jane'], 'Age' : [25, 18, 30]}pd.DataFrame(d, index=[10, 20, 30], columns=['First Name', 'Current Age'])

d = {'Name' : ['John', 'Adam', 'Jane'], 'Age' : [25, 18, 30]}pd.DataFrame(d, index=[10, 20, 30], columns=['First Name', 'Current Age'])

Image for post

從列表創建Pandas DataFrame: (Creating a Pandas DataFrame from a list:)

l = [['John', 25], ['Adam', 18], ['Jane', 30]]pd.DataFrame(l, columns=['Name', 'Age'])

l = [['John', 25], ['Adam', 18], ['Jane', 30]]pd.DataFrame(l, columns=['Name', 'Age'])

Image for post

從文件創建Pandas DataFrame (Creating a Pandas DataFrame from a File)

For any Data Science process, the dataset is commonly stored in files having formats like CSV (Comma Separated Values). Pandas allows storing data along with their labels from a CSV file using the method pandas.read_csv().

對于任何數據科學過程,數據集通常存儲在具有CSV(逗號分隔值)之類的格式的文件中。 Pandas允許使用pandas.read_csv()方法將數據及其標簽中的數據與CSV文件一起存儲

Image for post
Example1.csv
Example1.csv
Image for post

2.從Pandas DataFrame中選擇行和列 (2. Selecting Rows and Columns from a Pandas DataFrame)

從Pandas DataFrame中選擇列 (Selecting Columns from a Pandas DataFrame)

Columns can be selected using their column names.

可以使用列名稱選擇列。

df[column_1, column_2])

df[ column_1 , column_2 ])

Image for post
Selecting column ‘Name’ from DataFrame df
從DataFrame df中選擇“名稱”列

從Pandas DataFrame中選擇行 (Selecting Rows from a Pandas DataFrame)

Pandas provides 2 attributes for selecting rows from a DataFrame: loc and iloc

Pandas提供了2個用于從DataFrame中選擇行的屬性: lociloc

loc is label-based, which means that the row label has to be specified and iloc is integer-based which means that the integer index has to be specified.

loc是基于標簽的,這意味著必須指定行標簽,而iloc是基于整數的,這意味著必須指定整數索引。

Image for post
Using loc and iloc for selecting rows from DataFrame df
使用loc和iloc從DataFrame df中選擇行

3.在Pandas DataFrame中插入行和列 (3. Inserting Rows and Columns to a Pandas DataFrame)

在Pandas DataFrame中插入行 (Inserting Rows to a Pandas DataFrame)

One method of inserting a row into a DataFrame is to create a pandas.Series() object and insert it at the end of the DataFrame using the pandas.DataFrame.append()method. The column indices of the DataFrame serve as the index attribute for the Series object.

將行插入DataFrame的一種方法是創建pandas.Series() 對象,然后使用pandas.DataFrame.append()方法將其插入DataFrame的pandas.DataFrame.append() 。 DataFrame的列索引用作Series對象的索引屬性。

Image for post
Inserting new row to DataFrame df
將新行插入DataFrame df

將列插入Pandas DataFrame (Inserting Columns to a Pandas DataFrame)

One easy method of adding a column to a DataFrame is by just referring to the new column and assigning values.

將列添加到DataFrame的一種簡單方法是僅引用新列并分配值。

Image for post
Inserting columns ID, Score and Country to DataFrame df
將列ID,分數和國家/地區插入DataFrame df

4.從Pandas DataFrame刪除行和列 (4. Deleting Rows and Columns from a Pandas DataFrame)

從Pandas DataFrame刪除行 (Deleting Rows from a Pandas DataFrame)

A row can be deleted using the method pandas.DataFrame.drop() with it’s row label.

可以使用帶有行標簽的pandas.DataFrame.drop()方法刪除一行。

Image for post
Deleting row with label 1 from DataFrame df
從DataFrame df中刪除帶有標簽1的行

To delete a row based on a column, the index of the row is obtained using the DataFrame.index attribute and then the row with the index is deleted using the pandas.DataFrame.drop() method.

要刪除基于列的行,請使用DataFrame.index屬性獲取該行的索引,然后使用pandas.DataFrame.drop()方法刪除具有索引的行。

Image for post
Deleting row with Name Kelly from DataFrame df
從DataFrame df中刪除名稱為Kelly的行

從Pandas DataFrame刪除列 (Deleting Columns from a Pandas DataFrame)

A column can be deleted from a DataFrame based on its label as well as its position in the DataFrame using the method pandas.DataFrame.drop().

可以使用pandas.DataFrame.drop()方法根據列的標簽及其在DataFrame中的位置從DataFrame中刪除列

Image for post
Deleting column with label ‘Country’ from DataFrame df
從DataFrame df中刪除帶有標簽“國家”的列
Image for post
Deleting column with position 2 from DataFrame df
從DataFrame df中刪除位置2的列

The axis argument is set to 1 when dropping columns, and 0 when dropping rows.

刪除列時, axis參數設置為1;刪除行時, axis參數設置為0。

5.對Pandas DataFrame排序 (5. Sorting a Pandas DataFrame)

A Pandas DataFrame can be sorted using the pandas.DataFrame.sort_values() method. The by parameter for the method serves as the label of the column to sort by and ascending is set to True for sorting in ascending order and to False for sorting in descending order.

可以使用pandas.DataFrame.sort_values()方法對Pandas DataFrame進行排序。 該方法的by參數用作要按其進行排序的列的標簽,并且升序設置為True(以升序排序),設置為False(以降序排序)。

Image for post
Sorting DataFrame df by Name in ascending order
按名稱對DataFrame df進行升序排序
Image for post
Sorting DataFrame df by Age in descending order
按年齡降序對DataFrame df進行排序

https://www.datacamp.com/community/tutorials/pandas-tutorial-dataframe-pythonhttps://realpython.com/pandas-dataframe/#creating-a-pandas-dataframehttps://www.tutorialspoint.com/python_pandas/python_pandas_dataframe.htmhttps://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html

https://www.datacamp.com/community/tutorials/pandas-tutorial-dataframe-python https://realpython.com/pandas-dataframe/#creating-a-pandas-dataframe https://www.tutorialspoint.com/python_pandas/python_pandas_dataframe.htm https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html

翻譯自: https://medium.com/ml-course-microsoft-udacity/5-fundamental-operations-on-a-pandas-dataframe-93b4384dff9d

熊貓數據集

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/389646.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/389646.shtml
英文地址,請注明出處:http://en.pswp.cn/news/389646.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

圖嵌入綜述 (arxiv 1709.07604) 譯文五、六、七

應用 圖嵌入有益于各種圖分析應用,因為向量表示可以在時間和空間上高效處理。 在本節中,我們將圖嵌入的應用分類為節點相關,邊相關和圖相關。 節點相關應用 節點分類 節點分類是基于從標記節點習得的規則,為圖中的每個節點分配類標…

聊聊自動化測試框架

無論是在自動化測試實踐,還是日常交流中,經常聽到一個詞:框架。之前學習自動化測試的過程中,一直對“框架”這個詞知其然不知其所以然。 最近看了很多自動化相關的資料,加上自己的一些實踐,算是對“框架”有…

1971. Find if Path Exists in Graph

1971. Find if Path Exists in Graph 有一個具有 n個頂點的 雙向 圖,其中每個頂點標記從 0 到 n - 1(包含 0 和 n - 1)。圖中的邊用一個二維整數數組 edges 表示,其中 edges[i] [ui, vi] 表示頂點 ui 和頂點 vi 之間的雙向邊。 …

移動磁盤文件或目錄損壞且無法讀取資料如何找回

文件或目錄損壞且無法讀取說明這個盤的文件系統結構損壞了。在平時如果數據不重要,那么可以直接格式化就能用了。但是有的時候里面的數據很重要,那么就必須先恢復出數據再格式化。具體恢復方法可以看正文了解(不格式化的恢復方法)…

python 平滑時間序列_時間序列平滑以實現更好的聚類

python 平滑時間序列In time series analysis, the presence of dirty and messy data can alter our reasonings and conclusions. This is true, especially in this domain, because the temporal dependency plays a crucial role when dealing with temporal sequences.在…

基于SmartQQ協議的QQ自動回復機器人-1

0. 本項目的原始代碼及我二次開發后的代碼 1. 軟件安裝:【myeclipse6.0 maven2】 0. https://blog.csdn.net/zgmzyr/article/details/6886440 1. https://blog.csdn.net/shuzhe66/article/details/45009175 2. https://www.cnblogs.com/whgk/p/7112560.html<mirror><…

1725. 可以形成最大正方形的矩形數目

1725. 可以形成最大正方形的矩形數目 給你一個數組 rectangles &#xff0c;其中 rectangles[i] [li, wi] 表示第 i 個矩形的長度為 li 、寬度為 wi 。 如果存在 k 同時滿足 k < li 和 k < wi &#xff0c;就可以將第 i 個矩形切成邊長為 k 的正方形。例如&#xff0c…

幫助學生改善學習方法_學生應該如何花費時間改善自己的幸福

幫助學生改善學習方法There have been numerous studies looking into the relationship between sleep, exercise, leisure, studying and happiness. The results were often quite like how we expected, though there have been debates about the relationship between sl…

Spring Boot 靜態資源訪問原理解析

一、前言 springboot配置靜態資源方式是多種多樣&#xff0c;接下來我會介紹其中幾種方式&#xff0c;并解析一下其中的原理。 二、使用properties屬性進行配置 應該說 spring.mvc.static-path-pattern 和 spring.resources.static-locations這兩屬性是成對使用的&#xff0c;如…

深挖“窄帶高清”的實現原理

過去幾年&#xff0c;又拍云一直在點播、直播等視頻應用方面潛心鉆研&#xff0c;取得了不俗的成果。我們結合點播、直播、短視頻等業務中的用戶場景&#xff0c;推出了“省帶寬、壓成本”系列文章&#xff0c;從編碼技術、網絡架構等角度出發&#xff0c;結合又拍云的產品成果…

學習總結5 - bootstrap學習記錄1__安裝

1.bootstrap是什么&#xff1f; 簡潔、直觀、強悍的前端開發框架&#xff0c;說白了就是給后端二把刀開發網頁用的&#xff0c;讓web開發更迅速、簡單。 復制代碼 2.如何使用&#xff1f; 如圖所示到bootstrap中文網進行下載 復制代碼 下載完成之后&#xff0c;如圖所示&#x…

519. 隨機翻轉矩陣

519. 隨機翻轉矩陣 給你一個 m x n 的二元矩陣 matrix &#xff0c;且所有值被初始化為 0 。請你設計一個算法&#xff0c;隨機選取一個滿足 matrix[i][j] 0 的下標 (i, j) &#xff0c;并將它的值變為 1 。所有滿足 matrix[i][j] 0 的下標 (i, j) 被選取的概率應當均等。 …

模型的搜索和優化方法綜述:

一、常用的優化方法&#xff1a; 1.爬山 2.最陡峭下降 3.期望最大值 二、常用的搜索方法&#xff1a; 1.貪婪搜索 2.分支界定 3.寬度&#xff08;深度&#xff09;優先遍歷轉載于:https://www.cnblogs.com/xyp666/p/9042143.html

Redis 服務安裝

下載 客戶端可視化工具: RedisDesktopManager redis官網下載: http://redis.io/download windos服務安裝 windows服務安裝/卸載下載文件并解壓使用 管理員身份 運行命令行并且切換到解壓目錄執行 redis-service --service-install windowsR 打開運行窗口, 輸入 services.msc 查…

熊貓數據集_對熊貓數據框使用邏輯比較

熊貓數據集P (tPYTHON) Logical comparisons are used everywhere.邏輯比較隨處可見 。 The Pandas library gives you a lot of different ways that you can compare a DataFrame or Series to other Pandas objects, lists, scalar values, and more. The traditional comp…

初級功能筆試題-1

給我徒弟整理的一些理論性的筆試題&#xff0c;不喜勿噴。&#xff08;所以沒有答案哈&#xff09; 1、測試人員返測缺陷時&#xff0c;如果缺陷未修復&#xff0c;把缺陷的狀態置為下列什么狀態&#xff08;&#xff09;。 2、當驗證被測系統的主要業務流程和功能是否實現時&a…

ansbile--playbook劇本案例

個人博客轉至&#xff1a; www.zhangshoufu.com 通過ansible批量管理三臺服務器&#xff0c;使三臺服務器實現備份&#xff0c;web01、nfs、backup&#xff0c;把web和nfs上的重要文件被分到backup上&#xff0c;主機ip地址分配如下 CharacterIP地址IP地址主機名Rsync--server1…

5938. 找出數組排序后的目標下標

5938. 找出數組排序后的目標下標 給你一個下標從 0 開始的整數數組 nums 以及一個目標元素 target 。 目標下標 是一個滿足 nums[i] target 的下標 i 。 將 nums 按 非遞減 順序排序后&#xff0c;返回由 nums 中目標下標組成的列表。如果不存在目標下標&#xff0c;返回一…

決策樹之前要不要處理缺失值_不要使用這樣的決策樹

決策樹之前要不要處理缺失值As one of the most popular classic machine learning algorithm, the Decision Tree is much more intuitive than the others for its explainability. In one of my previous article, I have introduced the basic idea and mechanism of a Dec…

說說 C 語言中的變量與算術表達式

我們先來寫一個程序&#xff0c;打印英里與公里之間的對應關系表。公式&#xff1a;1 mile1.61 km 程序如下&#xff1a; #include <stdio.h>/* print Mile to Kilometre table*/ main() {float mile, kilometre;int lower 0;//lower limitint upper 1000;//upper limi…