แผ่นโกง Pandas สำหรับวิทยาศาสตร์ข้อมูลใน Python

แผ่นโกงแพนด้า

แผ่นโกง Pandas คืออะไร?

ห้องสมุด Pandas มีฟังก์ชั่นมากมาย แต่บางฟังก์ชั่นก็สร้างความสับสนให้กับบางคน เราได้จัดเตรียมแหล่งข้อมูลที่เป็นประโยชน์ไว้ที่นี่ซึ่งเรียกว่า Python แผ่นโกงแพนด้า มันอธิบายพื้นฐานของแพนด้าในลักษณะที่ง่ายและกระชับ

ไม่ว่าคุณจะเป็นมือใหม่หรือมีประสบการณ์กับ Pandas เอกสารสรุปนี้สามารถใช้เป็นแนวทางอ้างอิงที่เป็นประโยชน์ได้ โดยครอบคลุมหัวข้อต่างๆ มากมาย รวมถึงการทำงานกับโครงสร้างข้อมูล Series และ DataFrame การเลือกและการจัดลำดับข้อมูล และการใช้ฟังก์ชันกับข้อมูลของคุณ

สรุปแล้วแพนด้าตัวนี้ Python Cheat Sheet เป็นแหล่งข้อมูลที่ดีสำหรับทุกคนที่ต้องการเรียนรู้เพิ่มเติมเกี่ยวกับการใช้งาน Python สำหรับวิทยาศาสตร์ข้อมูล เป็นเครื่องมืออ้างอิงที่มีประโยชน์ มันสามารถช่วยให้คุณปรับปรุงของคุณได้ ทักษะการวิเคราะห์ข้อมูล และทำงานร่วมกับแพนด้าได้อย่างมีประสิทธิภาพมากขึ้น

👉 ดาวน์โหลด PDF ของ Cheat Sheet ที่นี่

อธิบายหน้าที่สำคัญของหมีแพนด้า:

หากต้องการเริ่มทำงานกับฟังก์ชันแพนด้า คุณต้องติดตั้งและนำเข้าแพนด้า มีสองคำสั่งในการดำเนินการนี้:

ขั้นตอนที่ 1) # ติดตั้ง Pandas

Pip ติดตั้งแพนด้า

ขั้นตอนที่ 2) # นำเข้านุ่น

นำเข้าแพนด้าเป็น pd

ตอนนี้คุณสามารถเริ่มทำงานกับฟังก์ชัน Pandas ได้แล้ว เราจะดำเนินการจัดการ วิเคราะห์ และทำความสะอาดข้อมูล หน้าที่สำคัญของแพนด้ามีดังนี้

โครงสร้างข้อมูลนุ่น

ดังที่เราได้กล่าวไปแล้วว่า Pandas มีโครงสร้างข้อมูลสองโครงสร้างที่เรียกว่า Series และ DataFrames ทั้งสองมีป้ายกำกับอาร์เรย์และสามารถเก็บข้อมูลประเภทใดก็ได้ มีความแตกต่างเพียงอย่างเดียวที่ Series เป็นอาร์เรย์หนึ่งมิติ และ DataFrame เป็นอาร์เรย์สองมิติ

1. ซีรีส์

มันเป็นอาร์เรย์ที่มีป้ายกำกับหนึ่งมิติ สามารถเก็บข้อมูลประเภทใดก็ได้

s = pd.Series([2, -4, 6, 3, None], index=['A', 'B', 'C', 'D', 'E'])

2. ดาต้าเฟรม

มันเป็นอาร์เรย์ที่มีป้ายกำกับสองมิติ สามารถเก็บประเภทข้อมูลและคอลัมน์ขนาดต่างๆ ได้

data = {'RollNo' : [101, 102, 75, 99],
        'Name' : ['Mithlesh', 'Ram', 'Rudra', 'Mithlesh'],
        'Course' : ['Nodejs', None, 'Nodejs', 'JavaScript']
}
df = pd.DataFrame(data, columns=['RollNo', 'Name', 'Course'])
df.head()

แผ่นโกงแพนด้า

การนำเข้าข้อมูล

Pandas มีความสามารถในการนำเข้าหรืออ่านไฟล์ประเภทต่างๆ ใน ​​Notebook ของคุณ

นี่คือตัวอย่างบางส่วนที่ระบุด้านล่าง

# Import a CSV file pd
pd.read_csv(filename)

# Import a TSV file
pd.read_table(filename)

# Import a Excel file pd
pd.read_excel(filename)

# Import a SQL table/database
pd.read_sql(query, connection_object)

# Import a JSON file
pd.read_json(json_string)

# Import a HTML file
pd.read_html(url)

# From clipboard to read_table()
pd.read_clipboard()

# From dict
pd.DataFrame(dict)

การเลือก

คุณสามารถเลือกองค์ประกอบตามตำแหน่งหรือดัชนีได้ คุณสามารถเลือกแถว คอลัมน์ และค่าที่แตกต่างกันได้โดยใช้เทคนิคเหล่านี้

1. ซีรีส์

# Accessing one element from Series
s['D']

# Accessing all elements between two given indices
s['A':'C']

# Accessing all elements from starting till given index
s[:'C']

# Accessing all elements from given index till end
s['B':]

2. ดาต้าเฟรม

# Accessing one column df
df['Name']

# Accessing rows from after given row
df[1:]

# Accessing till before given row
df[:1]

# Accessing rows between two given rows
df[1:2]

การเลือกโดยการจัดทำดัชนีและการตั้งค่าบูลีน

1. ตามตำแหน่ง

df.iloc[0, 1]

df.iat[0, 1]

2. ตามฉลาก

df.loc[[0],  ['Name']]

3. ตามป้ายกำกับ/ตำแหน่ง

df.loc[2] # Both are same
df.iloc[2]

4. การจัดทำดัชนีบูลีน

# Series s where value is > 1
s[(s > 0)]

# Series s where value is <-2 or >1
s[(s < -2) | ~(s > 1)]

# Use filter to adjust DataFrame
df[df['RollNo']>100]

# Set index a of Series s to 6
s['D'] = 10
s.head()

การทำความสะอาดข้อมูล

สำหรับ Python วัตถุประสงค์ในการทำความสะอาดข้อมูล คุณสามารถดำเนินการต่อไปนี้ได้:

  • เปลี่ยนชื่อคอลัมน์โดยใช้เมธอด rename()
  • อัปเดตค่าโดยใช้เมธอด at[] หรือ iat[] เพื่อเข้าถึงและแก้ไของค์ประกอบเฉพาะ
  • สร้างสำเนาของซีรี่ส์หรือเฟรมข้อมูลโดยใช้วิธี copy()
  • ตรวจสอบค่า NULL โดยใช้วิธี isnull() และปล่อยค่าเหล่านั้นโดยใช้วิธี dropna()
  • ตรวจสอบค่าที่ซ้ำกันโดยใช้วิธีการซ้ำกัน () วางพวกมันโดยใช้เมธอด drop_duplicates()
  • แทนที่ค่า NULL โดยใช้วิธีเติม () ด้วยค่าที่ระบุ
  • แทนที่ค่าโดยใช้เมธอดแทนที่ ()
  • จัดเรียงค่าโดยใช้เมธอด sort_values()
  • จัดอันดับค่าโดยใช้วิธี rank()
# Renaming columns
df.columns = ['a','b','c']
df.head()

# Mass renaming of columns
df = df.rename(columns={'RollNo': 'ID', 'Name': 'Student_Name'})

# Or use this edit in same DataFrame instead of in copy
df.rename(columns={'RollNo': 'ID', 'Name': 'Student_Name'}, inplace=True)
df.head()

# Counting duplicates in a column
df.duplicated(subset='Name')

# Removing entire row that has duplicate in given column
df.drop_duplicates(subset=['Name'])

# You can choose which one keep - by default is first
df.drop_duplicates(subset=['Name'], keep='last')

# Checks for Null Values
s.isnull()

# Checks for non-Null Values - reverse of isnull()
s.notnull()

# Checks for Null Values df
df.isnull()

# Checks for non-Null Values - reverse of isnull()
df.notnull()

# Drops all rows that contain null values
df.dropna()

# Drops all columns that contain null values
df.dropna(axis=1)

# Replaces all null values with 'Guru99'
df.fillna('Guru99')

# Replaces all null values with the mean
s.fillna(s.mean())

# Converts the datatype of the Series to float
s.astype(float)

# Replaces all values equal to 6 with 'Six'
s.replace(6,'Six')

# Replaces all 2 with 'Two' and 6 with 'Six'
s.replace([2,6],['Two','Six'])

# Drop from rows (axis=0)
s.drop(['B',  'D'])

# Drop from columns(axis=1)
df.drop('Name', axis=1)

# Sort by labels with axis
df.sort_index()

# Sort by values with axis
df.sort_values(by='RollNo')

# Ranking entries
df.rank()

# s1 is pointing to same Series as s
s1 = s

# s_copy of s, but not pointing same Series
s_copy = s.copy()

# df1 is pointing to same DataFrame as df
df1 = s

# df_copy of df, but not pointing same DataFrame
df_copy = df.copy()

การดึงข้อมูล

คุณสามารถดำเนินการต่อไปนี้เพื่อค้นหาข้อมูล:

  • ใช้แอตทริบิวต์รูปร่างเพื่อรับจำนวนแถวและคอลัมน์
  • ใช้เมธอด head() หรือ tail() เพื่อรับแถวแรกหรือสองสามแถวสุดท้ายเป็นตัวอย่าง
  • ใช้เมธอด info(), อธิบาย() หรือ dtypes เพื่อรับข้อมูลเกี่ยวกับประเภทข้อมูล การนับ ค่าเฉลี่ย ค่าเบี่ยงเบนมาตรฐาน ค่าต่ำสุด และค่าสูงสุด
  • ใช้เมธอด count(), min(), max(), sum(), mean() และ median() เพื่อรับข้อมูลทางสถิติเฉพาะสำหรับค่าต่างๆ
  • ใช้เมธอด loc[] เพื่อรับแถว
  • ใช้เมธอด groupby() เพื่อใช้ฟังก์ชัน GROUP BY เพื่อจัดกลุ่มค่าที่คล้ายกันในคอลัมน์ของ DataFrame

1 ข้อมูลพื้นฐาน

# Counting all elements in Series
len(s)

# Counting all elements in DataFrame
len(df)

# Prints number of rows and columns in dataframe
df.shape

# Prints first 10 rows by default, if no value set
df.head(10)

# Prints last 10 rows by default, if no value set
df.tail(10)

# For counting non-Null values column-wise
df.count()

# For range of index df
df.index

# For name of attributes/columns
df.columns

# Index, Data Type and Memory information
df.info()

# Datatypes of each column
df.dtypes

# Summary statistics for numerical columns
df.describe()

2 สรุป

# For adding all values column-wise
df.sum()

# For min column-wise
df.min()

# For max column-wise
df.max()

# For mean value in number column
df.mean()

# For median value in number column
df.median()

# Count non-Null values
s.count()

# Count non-Null values
df.count()

# Return Series of given column
df['Name'].tolist()

# Name of columns
df.columns.tolist()

# Creating subset
df[['Name', 'Course']]

# Return number of values in each group
df.groupby('Name').count()

การใช้ฟังก์ชั่น

# Define function
f = lambda x: x*5

# Apply this function on given Series - For each value
s.apply(f)

# Apply this function on given DataFrame - For each value
df.apply(f)

1. การจัดตำแหน่งข้อมูลภายใน

# NA values for indices that don't overlap
s2 = pd.Series([8, -1, 4],  index=['A',  'C',  'D'])
s + s2

2. เลขคณิต Operaด้วยวิธีเติม

# Fill values that don't overlap
s.add(s2, fill_value=0)

3. กรอง จัดเรียง และจัดกลุ่มตาม

ฟังก์ชันต่อไปนี้ใช้สำหรับการกรอง การเรียงลำดับ และการจัดกลุ่มตาม Series และ DataFrame

# Filter rows where column is greater than 100
df[df['RollNo']>100]

# Filter rows where 70 < column < 101
df[(df['RollNo'] > 70) & (df['RollNo'] < 101)]

# Sorts values in ascending order
s.sort_values()

# Sorts values in descending order
s.sort_values(ascending=False)

# Sorts values by RollNo in ascending order
df.sort_values('RollNo')

# Sorts values by RollNo in descending order
df.sort_values('RollNo', ascending=False)

การส่งออกข้อมูล

Pandas มีความสามารถในการส่งออกหรือเขียนข้อมูลในรูปแบบต่างๆ นี่คือตัวอย่างบางส่วนที่ระบุด้านล่าง

# Export as a CSV file df
df.to_csv(filename)

# Export as a Excel file df
df.to_excel(filename)

# Export as a SQL table df
df.to_sql(table_name, connection_object)

# Export as a JSON file
df.to_json(filename)

# Export as a HTML table
df.to_html(filename)

# Write to the clipboard
df.to_clipboard()

สรุปสูตรโกงของ Pandas:

นุ่น เป็นห้องสมุดโอเพ่นซอร์สใน Python สำหรับการทำงานกับชุดข้อมูล ความสามารถในการวิเคราะห์ ทำความสะอาด สำรวจ และจัดการข้อมูล Pandas ถูกสร้างขึ้นบน Numpy ใช้กับโปรแกรมอื่นๆ เช่น Matplotlib และ Scikit-เรียนรู้- โดยครอบคลุมหัวข้อต่างๆ เช่น โครงสร้างข้อมูล การเลือกข้อมูล การนำเข้าข้อมูล การทำดัชนีบูลีน ค่าที่ลดลง การเรียงลำดับ และการล้างข้อมูล เราได้เตรียมเอกสารโกงแพนด้าในรูปแบบ pdf สำหรับบทความด้วย Pandas เป็นห้องสมุดใน Python และวิทยาศาสตร์ข้อมูลใช้ไลบรารีนี้เพื่อทำงานกับดาต้าเฟรมและซีรีส์ของแพนด้า เราได้กล่าวถึงคำสั่งต่างๆ ของแพนด้าแล้วในเอกสารสรุปนี้

Colab ของ Cheat Sheet

ไฟล์แบบฝึกหัด Colab ของฉันสำหรับแพนด้า – แผ่นโกงแพนด้า – Python สำหรับ Data Science.ipynb