23 great Pandas codes for Data Scientists

source:https://towardsdatascience.com/23-great-pandas-codes-for-data-scientists-cca5ed9d8a38

基本數據集信息

(1)讀取CSV數據

 pd.DataFrame.from_csv(“csv_file”) 

或者

 pd.read_csv(“csv_file”)

(2)讀取Excel數據

 pd.read_excel("excel_file")

(3)將數據直接寫入csv
df.to_csv("data.csv", sep=",", index=False) #以逗號隔開,無索引
(4)數據特徵的基本信息

df.info()

(5)數據的統計信息

print(df.describe())

(6)將數據打印爲表格

print(tabulate(print_table, headers=headers)) 

where “print_table” is a list of lists and “headers” is a list of the string headers
(7)打印列名

df.columns

基本數據處理

(1)刪除缺失值

df.dropna(axis=0, how='any')

Returns object with labels on given axis omitted where alternately any or all of the data are missing
(2)替換缺失值

df.replace(to_replace=None, value=None)

replaces values given in “to_replace” with “value”.
(3)檢查空值

pd.isnull(object)

Detect missing values (NaN in numeric arrays, None/NaN in object arrays)
(4)刪除特徵

df.drop('feature_variable_name', axis=1)

axis is either 0 for rows, 1 for columns
(5)將對象轉換爲float

pd.to_numeric(df["feature_name"], errors='coerce')

Convert object types to numeric to be able to perform computations (in case they are string)
(6)將數據轉換爲numpy數組

df.as_matrix()

(7)打印前n行數據

df.head(n)

(8)根據特徵名稱得到數據

df.loc[feature_name]

數據的操作

(1)對一組數據進行函數變換
This one will multiple all values in the “height” column of the data frame by 2

df["height"].apply(lambda height: 2 * height)

OR

def multiply(x):
    return x * 2
df["height"].apply(multiply)

(2)對列進行重命名
Here we will rename the 3rd column of the data frame to be called “size”

df.rename(columns = {df.columns[2]:'size'}, inplace=True)

(3)獲取列的唯一項
Here we will get the unique entries of the column “name”

df["name"].unique()

(4)訪問數據子集
Here we’ll grab a selection of the columns, “name” and “size” from the data frame

new_df = df[["name", "size"]]

(5)獲取數據的基本信息

# Sum of values in a data frame
df.sum()
# Lowest value of a data frame
df.min()
# Highest value
df.max()
# Index of the lowest value
df.idxmin()
# Index of the highest value
df.idxmax()
# Statistical summary of the data frame, with quartiles, median, etc.
df.describe()
# Average values
df.mean()
# Median values
df.median()
# Correlation between columns
df.corr()
# To get these values for only one column, just select it like this#
df["size"].median()

(6)對數據排序

df.sort_values(ascending = False)

(7)布爾索引
Here we’ll filter our data column named “size” to show only values equal to 5

df.loc([0], ['size'])
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章