这是用户在 2025-5-6 14:59 为 http://localhost:8888/notebooks/Untitled8.ipynb? 保存的双语快照页面,由 沉浸式翻译 提供双语支持。了解如何保存?

Untitled8.ipynb

No Headings

The table of contents shows headings in notebooks and supported files.

Skip to Main
Jupyter

Untitled8

Last Checkpoint: 4 hours ago
  • File
  • Edit
  • View
  • Run
  • Kernel
  • Settings
  • Help
Kernel status: Idle
[29]:
import pandas as pd
import os

# 指定文件路径
file_path = r"C:\Users\ASUS\Desktop\大二下课程\python数据处理\3\iris.txt"
student_id = "2023014925"

# 读取iris.txt文件,该文件没有表头
iris_data = pd.read_csv(file_path, header=None, sep=",")
print("姓名:丁晴")
print("学号:2023014925")
print(iris_data)

# 添加表头信息
headers = ["Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.width", "Species"]
iris_data.columns = headers

# 显示前几行数据,确认读取和添加表头正确
print("姓名:丁晴")
print("学号:2023014925")
print("读取的数据及添加表头后的结果:")
print(iris_data)

# 保存为CSV文件,以学号命名
output_file = os.path.join(os.path.dirname(file_path), f"{student_id}.csv")
iris_data.to_csv(output_file, index=False)
print("姓名:丁晴")
print("学号:2023014925")
print(f"\n数据已保存为: {output_file}")
姓名:丁晴
学号:2023014925
       0    1    2    3               4
0    5.1  3.5  1.4  0.2     Iris-setosa
1    4.9  3.0  1.4  0.2     Iris-setosa
2    4.7  3.2  1.3  0.2     Iris-setosa
3    4.6  3.1  1.5  0.2     Iris-setosa
4    5.0  3.6  1.4  0.2     Iris-setosa
..   ...  ...  ...  ...             ...
145  6.7  3.0  5.2  2.3  Iris-virginica
146  6.3  2.5  5.0  1.9  Iris-virginica
147  6.5  3.0  5.2  2.0  Iris-virginica
148  6.2  3.4  5.4  2.3  Iris-virginica
149  5.9  3.0  5.1  1.8  Iris-virginica

[150 rows x 5 columns]
姓名:丁晴
学号:2023014925
读取的数据及添加表头后的结果:
     Sepal.Length  Sepal.Width  Petal.Length  Petal.width         Species
0             5.1          3.5           1.4          0.2     Iris-setosa
1             4.9          3.0           1.4          0.2     Iris-setosa
2             4.7          3.2           1.3          0.2     Iris-setosa
3             4.6          3.1           1.5          0.2     Iris-setosa
4             5.0          3.6           1.4          0.2     Iris-setosa
..            ...          ...           ...          ...             ...
145           6.7          3.0           5.2          2.3  Iris-virginica
146           6.3          2.5           5.0          1.9  Iris-virginica
147           6.5          3.0           5.2          2.0  Iris-virginica
148           6.2          3.4           5.4          2.3  Iris-virginica
149           5.9          3.0           5.1          1.8  Iris-virginica

[150 rows x 5 columns]
姓名:丁晴
学号:2023014925

数据已保存为: C:\Users\ASUS\Desktop\大二下课程\python数据处理\3\2023014925.csv
[9]:
iris_data.columns = ['Sepal.Length', 'Sepal.Width', 'Petal.Length', 'Petal.Width', 'Species']
iris_data
[9]:
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
0 5.1 3.5 1.4 0.2 Iris-setosa
1 4.9 3.0 1.4 0.2 Iris-setosa
2 4.7 3.2 1.3 0.2 Iris-setosa
3 4.6 3.1 1.5 0.2 Iris-setosa
4 5.0 3.6 1.4 0.2 Iris-setosa
... ... ... ... ... ...
145 6.7 3.0 5.2 2.3 Iris-virginica
146 6.3 2.5 5.0 1.9 Iris-virginica
147 6.5 3.0 5.2 2.0 Iris-virginica
148 6.2 3.4 5.4 2.3 Iris-virginica
149 5.9 3.0 5.1 1.8 Iris-virginica

150 rows × 5 columns

[19]:
iris_data.to_csv('2023014925.csv')
[1]:
file1_path = r"C:\Users\ASUS\Desktop\大二下课程\python数据处理\3\iris_NaN.csv"
file2_path = r"C:\Users\ASUS\Desktop\大二下课程\python数据处理\3\2023014925.csv"
df1 = pd.read_csv(file1_path)
df2 = pd.read_csv(file2_path)
print("DataFrame 1 columns:", df1.columns)
print("DataFrame 2 columns:", df2.columns)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[1], line 3
      1 file1_path = r"C:\Users\ASUS\Desktop\大二下课程\python数据处理\3\iris_NaN.csv"
      2 file2_path = r"C:\Users\ASUS\Desktop\大二下课程\python数据处理\3\2023014925.csv"
----> 3 df1 = pd.read_csv(file1_path)  
      4 df2 = pd.read_csv(file2_path)
      5 print("DataFrame 1 columns:", df1.columns)

NameError: name 'pd' is not defined
[37]:
import pandas as pd
import os

# 指定文件路径
base_path = r"C:\Users\ASUS\Desktop\大二下课程\python数据处理\3"
student_id = "2023014925"

# 读取第一个文件:自己创建的CSV文件
file1_path = os.path.join(base_path, f"{student_id}.csv")
df1 = pd.read_csv(file1_path)

# 读取第二个文件:iris_NaN.csv
file2_path = os.path.join(base_path, "iris_NaN.csv")
df2 = pd.read_csv(file2_path)

# 显示读取的两个DataFrame的前几行
print("DataFrame 1 (前5行):")
print(df1.head())
print("\nDataFrame 2 (前5行):")
print(df2.head())

# 使用merge方法根据Species列合并两个DataFrame
merged_df = pd.merge(df1, df2, on='Species', how='inner')

print("姓名:丁晴")
print("学号:2023014925")
# 显示合并后的DataFrame前几行
print("合并后的DataFrame :")
print(merged_df)

# 查看合并后DataFrame的形状(行数和列数)
print(f"\n合并后DataFrame的形状: {merged_df.shape}")
DataFrame 1 (前5行):
   Sepal.Length  Sepal.Width  Petal.Length  Petal.width      Species
0           5.1          3.5           1.4          0.2  Iris-setosa
1           4.9          3.0           1.4          0.2  Iris-setosa
2           4.7          3.2           1.3          0.2  Iris-setosa
3           4.6          3.1           1.5          0.2  Iris-setosa
4           5.0          3.6           1.4          0.2  Iris-setosa

DataFrame 2 (前5行):
   Feature_1  Feature_2  Feature_3  Feature_4      Species
0        5.1        3.5        1.4        0.2  Iris-setosa
1        4.9        3.0        1.4        0.2  Iris-setosa
2        4.7        3.2        1.3        0.2  Iris-setosa
3        4.6        3.1        1.5        0.2  Iris-setosa
4        5.0        3.6        1.4        0.2  Iris-setosa
姓名:丁晴
学号:2023014925
合并后的DataFrame :
      Sepal.Length  Sepal.Width  Petal.Length  Petal.width         Species  \
0              5.1          3.5           1.4          0.2     Iris-setosa   
1              5.1          3.5           1.4          0.2     Iris-setosa   
2              5.1          3.5           1.4          0.2     Iris-setosa   
3              5.1          3.5           1.4          0.2     Iris-setosa   
4              5.1          3.5           1.4          0.2     Iris-setosa   
...            ...          ...           ...          ...             ...   
7495           5.9          3.0           5.1          1.8  Iris-virginica   
7496           5.9          3.0           5.1          1.8  Iris-virginica   
7497           5.9          3.0           5.1          1.8  Iris-virginica   
7498           5.9          3.0           5.1          1.8  Iris-virginica   
7499           5.9          3.0           5.1          1.8  Iris-virginica   

      Feature_1  Feature_2  Feature_3  Feature_4  
0           5.1        3.5        1.4        0.2  
1           4.9        3.0        1.4        0.2  
2           4.7        3.2        1.3        0.2  
3           4.6        3.1        1.5        0.2  
4           5.0        3.6        1.4        0.2  
...         ...        ...        ...        ...  
7495        6.7        3.0        5.2        2.3  
7496        6.3        2.5        5.0        1.9  
7497        6.5        3.0        5.2        2.0  
7498        6.2        3.4        5.4        2.3  
7499        5.9        3.0        5.1        1.8  

[7500 rows x 9 columns]

合并后DataFrame的形状: (7500, 9)
[3]:
Selection deleted
import pandas as pd
import os

# 指定文件路径
base_path = r"C:\Users\ASUS\Desktop\大二下课程\python数据处理\3"
student_id = "2023014925"

# 读取第一个文件:自己创建的CSV文件
file1_path = os.path.join(base_path, f"{student_id}.csv")
df1 = pd.read_csv(file1_path)

# 读取第二个文件:iris_NaN.csv
file2_path = os.path.join(base_path, "iris_NaN.csv")
df2 = pd.read_csv(file2_path)

# 显示读取的两个DataFrame的形状
print(f"DataFrame 1的形状: {df1.shape}")
print(f"DataFrame 2的形状: {df2.shape}")

# 使用concat方法沿着行方向(axis=0)连接两个DataFrame
concatenated_df = pd.concat([df1, df2], axis=1)
print("姓名:丁晴")
print("学号:2023014925")
# 显示连接后的DataFrame的前几行和后几行
print("连接后DataFrame:")
print(concatenated_df)

# 查看连接后DataFrame的形状(行数和列数)
print(f"\n连接后DataFrame的形状: {concatenated_df.shape}")
DataFrame 1的形状: (150, 5)
DataFrame 2的形状: (150, 5)
姓名:丁晴
学号:2023014925
连接后DataFrame:
     Sepal.Length  Sepal.Width  Petal.Length  Petal.width         Species  \
0             5.1          3.5           1.4          0.2     Iris-setosa   
1             4.9          3.0           1.4          0.2     Iris-setosa   
2             4.7          3.2           1.3          0.2     Iris-setosa   
3             4.6          3.1           1.5          0.2     Iris-setosa   
4             5.0          3.6           1.4          0.2     Iris-setosa   
..            ...          ...           ...          ...             ...   
145           6.7          3.0           5.2          2.3  Iris-virginica   
146           6.3          2.5           5.0          1.9  Iris-virginica   
147           6.5          3.0           5.2          2.0  Iris-virginica   
148           6.2          3.4           5.4          2.3  Iris-virginica   
149           5.9          3.0           5.1          1.8  Iris-virginica   

     Feature_1  Feature_2  Feature_3  Feature_4         Species  
0          5.1        3.5        1.4        0.2     Iris-setosa  
1          4.9        3.0        1.4        0.2     Iris-setosa  
2          4.7        3.2        1.3        0.2     Iris-setosa  
3          4.6        3.1        1.5        0.2     Iris-setosa  
4          5.0        3.6        1.4        0.2     Iris-setosa  
..         ...        ...        ...        ...             ...  
145        6.7        3.0        5.2        2.3  Iris-virginica  
146        6.3        2.5        5.0        1.9  Iris-virginica  
147        6.5        3.0        5.2        2.0  Iris-virginica  
148        6.2        3.4        5.4        2.3  Iris-virginica  
149        5.9        3.0        5.1        1.8  Iris-virginica  

[150 rows x 10 columns]

连接后DataFrame的形状: (150, 10)
[53]:
# a. 使用isnull().sum()检查是否有缺失值
print("检查缺失值:")
missing_values = concatenated_df.isnull().sum()
print(missing_values)
检查缺失值:
Sepal.Length    150
Sepal.Width     150
Petal.Length    150
Petal.width     150
Species           0
Feature_1       150
Feature_2       191
Feature_3       191
Feature_4       150
dtype: int64
[51]:
print("姓名:丁晴")
print("学号:2023014925")
df_dropped = concatenated_df.dropna()
print("\n删除缺失值后的DataFrame形状:")
print(f"原始形状: {concatenated_df.shape}, 删除缺失值后: {df_dropped.shape}")
print(f"删除了 {concatenated_df.shape[0] - df_dropped.shape[0]} 行")
df_dropped
姓名:丁晴
学号:2023014925

删除缺失值后的DataFrame形状:
原始形状: (300, 9), 删除缺失值后: (0, 9)
删除了 300 行
[51]:
Sepal.Length Sepal.Width Petal.Length Petal.width Species Feature_1 Feature_2 Feature_3 Feature_4
[55]:
# c. 使用学号最后一位对缺失值进行填充
df_filled = concatenated_df.fillna(fill_value)
print(f"\n使用学号最后一位 {fill_value} 填充缺失值后的DataFrame:")
print(df_filled.head(10)) # 显示前10行,应该可以看到一些被填充的值

# 确认填充后没有缺失值
print("\n填充后检查缺失值:")
print(df_filled.isnull().sum())
使用学号最后一位 5 填充缺失值后的DataFrame:
   Sepal.Length  Sepal.Width  Petal.Length  Petal.width      Species  \
0           5.1          3.5           1.4          0.2  Iris-setosa   
1           4.9          3.0           1.4          0.2  Iris-setosa   
2           4.7          3.2           1.3          0.2  Iris-setosa   
3           4.6          3.1           1.5          0.2  Iris-setosa   
4           5.0          3.6           1.4          0.2  Iris-setosa   
5           5.4          3.9           1.7          0.4  Iris-setosa   
6           4.6          3.4           1.4          0.3  Iris-setosa   
7           5.0          3.4           1.5          0.2  Iris-setosa   
8           4.4          2.9           1.4          0.2  Iris-setosa   
9           4.9          3.1           1.5          0.1  Iris-setosa   

   Feature_1  Feature_2  Feature_3  Feature_4  
0        5.0        5.0        5.0        5.0  
1        5.0        5.0        5.0        5.0  
2        5.0        5.0        5.0        5.0  
3        5.0        5.0        5.0        5.0  
4        5.0        5.0        5.0        5.0  
5        5.0        5.0        5.0        5.0  
6        5.0        5.0        5.0        5.0  
7        5.0        5.0        5.0        5.0  
8        5.0        5.0        5.0        5.0  
9        5.0        5.0        5.0        5.0  

填充后检查缺失值:
Sepal.Length    0
Sepal.Width     0
Petal.Length    0
Petal.width     0
Species         0
Feature_1       0
Feature_2       0
Feature_3       0
Feature_4       0
dtype: int64
[ ]:

-

Variables

Callstack

    Breakpoints

    Source

    9
    1

    Kernel Sources

    Common Tools
    No metadata.
    Advanced Tools
    No metadata.
    Anaconda Assistant
    AI-powered coding, insights and debugging in your notebooks.
    To enable the following extensions, create an account or sign in.
    • Anaconda Assistant
      4.1.0
    • Coming soon!
    • Data Catalogs
    • Panel Deployments
    • Sharing
    Already have an account? Sign In
    For more information, read our Anaconda Assistant documentation.
    • Assistant
    • Open Anaconda Assistant
      Ctrl+Shift+A
    • Console
    • Change Kernel…
    • Clear Console Cells
    • Close and Shut Down…
    • Insert Line Break
    • Interrupt Kernel
    • New Console
    • Restart Kernel…
    • Run Cell (forced)
    • Run Cell (unforced)
    • Show All Kernel Activity
    • Debugger
    • Breakpoints on exception
    • Evaluate Code
      Evaluate Code
    • Next
      Next
      F10
    • Pause
      Pause
      F9
    • Step In
      Step In
      F11
    • Step Out
      Step Out
      Shift+F11
    • Terminate
      Terminate
      Shift+F9
    • Display Languages
    • English
      English
    • File Operations
    • Autosave Documents
    • Download
      Download the file to your computer
    • Reload Notebook from Disk
      Reload contents from disk
    • Revert Notebook to Checkpoint…
      Revert contents to previous checkpoint
    • Save Notebook
      Save and create checkpoint
      Ctrl+S
    • Save Notebook As…
      Save with new path
      Ctrl+Shift+S
    • Trust HTML File
      Whether the HTML file is trusted. Trusting the file allows scripts to run in it, which may result in security risks. Only enable for files you trust.
    • Help
    • About Jupyter Notebook
    • Launch Jupyter Notebook File Browser
    • Show Keyboard Shortcuts
      Show relevant keyboard shortcuts for the current active widget
      Ctrl+Shift+H
    • Image Viewer
    • Flip image horizontally
      H
    • Flip image vertically
      V
    • Invert Colors
      I
    • Reset Image
      0
    • Rotate Clockwise
      ]
    • Rotate Counterclockwise
      [
    • Zoom In
      =
    • Zoom Out
      -
    • Kernel Operations
    • Shut Down All Kernels…
    • Main Area
    • Close All Other Tabs
    • Close Tab
      Alt+W
    • Close Tabs to Right
    • End Search
      Esc
    • Find Next
      Ctrl+G
    • Find Previous
      Ctrl+Shift+G
    • Find…
      Ctrl+F
    • Log Out
      Log out of JupyterLab
    • Shut Down
      Shut down JupyterLab
    • Mode
    • Toggle Zen Mode
    • Notebook Cell Operations
    • Change to Code Cell Type
      Y
    • Change to Heading 1
      1
    • Change to Heading 2
      2
    • Change to Heading 3
      3
    • Change to Heading 4
      4
    • Change to Heading 5
      5
    • Change to Heading 6
      6
    • Change to Markdown Cell Type
      M
    • Change to Raw Cell Type
      R
    • Clear Cell Output
      Clear outputs for the selected cells
    • Collapse All Code
    • Collapse All Outputs
    • Collapse Selected Code
    • Collapse Selected Outputs
    • Copy Cell
      Copy this cell
      C
    • Cut Cell
      Cut this cell
      X
    • Delete Cell
      Delete this cell
      D, D
    • Disable Scrolling for Outputs
    • Enable Scrolling for Outputs
    • Expand All Code
    • Expand All Outputs
    • Expand Selected Code
    • Expand Selected Outputs
    • Extend Selection Above
      Shift+K
    • Extend Selection Below
      Shift+J
    • Extend Selection to Bottom
      Shift+End
    • Extend Selection to Top
      Shift+Home
    • Insert Cell Above
      Insert a cell above
      A
    • Insert Cell Below
      Insert a cell below
      B
    • Insert Heading Above Current Heading
      Shift+A
    • Insert Heading Below Current Heading
      Shift+B
    • Merge Cell Above
      Ctrl+Backspace
    • Merge Cell Below
      Ctrl+Shift+M
    • Merge Selected Cells
      Shift+M
    • Move Cell Down
      Move this cell down
      Ctrl+Shift+Down
    • Move Cell Up
      Move this cell up
      Ctrl+Shift+Up
    • Paste Cell Above
      Paste this cell from the clipboard
    • Paste Cell and Replace
    • Paste Cell Below
      Paste this cell from the clipboard
      V
    • Redo Cell Operation
      Shift+Z
    • Render Side-by-Side
      Shift+R
    • Run Selected Cell
      Run this cell and advance
      Shift+Enter
    • Run Selected Cell and Do not Advance
      Ctrl+Enter
    • Run Selected Cell and Insert Below
      Alt+Enter
    • Run Selected Text or Current Line in Console
    • Select Cell Above
      K
    • Select Cell Below
      J
    • Select Heading Above or Collapse Heading
      Left
    • Select Heading Below or Expand Heading
      Right
    • Set side-by-side ratio
    • Split Cell
      Ctrl+Shift+-
    • Undo Cell Operation
      Z
    • Notebook Operations
    • Change Kernel…
    • Clear Outputs of All Cells
      Clear all outputs of all cells
    • Close and Shut Down Notebook
    • Collapse All Headings
      Ctrl+Shift+Left
    • Deselect All Cells
    • Enter Command Mode
      Ctrl+M
    • Enter Edit Mode
      Enter
    • Expand All Headings
      Ctrl+Shift+Right
    • Interrupt Kernel
      Interrupt the kernel
    • New Console for Notebook
    • New Notebook
      Create a new notebook
    • Open with Panel in New Browser Tab
    • Preview Notebook with Panel
    • Reconnect to Kernel
    • Render All Markdown Cells
    • Restart Kernel and Clear Outputs of All Cells…
      Restart the kernel and clear all outputs of all cells
    • Restart Kernel and Debug…
      Restart Kernel and Debug…
    • Restart Kernel and Run All Cells…
      Restart the kernel and run all cells
    • Restart Kernel and Run up to Selected Cell…
    • Restart Kernel…
      Restart the kernel
    • Run All Above Selected Cell
    • Run All Cells
      Run all cells
    • Run Selected Cell and All Below
    • Save and Export Notebook: Asciidoc
    • Save and Export Notebook: Executable Script
    • Save and Export Notebook: HTML
    • Save and Export Notebook: LaTeX
    • Save and Export Notebook: Markdown
    • Save and Export Notebook: PDF
    • Save and Export Notebook: Qtpdf
    • Save and Export Notebook: Qtpng
    • Save and Export Notebook: ReStructured Text
    • Save and Export Notebook: Reveal.js Slides
    • Save and Export Notebook: Webpdf
    • Select All Cells
      Ctrl+A
    • Show Line Numbers
    • Toggle Collapse Notebook Heading
    • Trust Notebook
    • Other
    • Open in JupyterLab
      JupyterLab
    • Terminal
    • Decrease Terminal Font Size
    • Increase Terminal Font Size
    • New Terminal
      Start a new terminal session
    • Refresh Terminal
      Refresh the current terminal session
    • Use Terminal Theme: Dark
      Set the terminal theme
    • Use Terminal Theme: Inherit
      Set the terminal theme
    • Use Terminal Theme: Light
      Set the terminal theme
    • Text Editor
    • Decrease Font Size
    • Increase Font Size
    • New Markdown File
      Create a new markdown file
    • New Python File
      Create a new Python file
    • New Text File
      Create a new text file
    • Spaces: 1
    • Spaces: 2
    • Spaces: 4
    • Spaces: 4
    • Spaces: 8
    • Theme
    • Decrease Code Font Size
    • Decrease Content Font Size
    • Decrease UI Font Size
    • Increase Code Font Size
    • Increase Content Font Size
    • Increase UI Font Size
    • Theme Scrollbars
    • Use Theme: JupyterLab Dark
    • Use Theme: JupyterLab Light
    • View
    • File Browser
    • Open JupyterLab
    • Show Anaconda Assistant
      Show Show Anaconda Assistant in the right sidebar
    • Show Debugger
      Show Show Debugger in the right sidebar
    • Show Header
    • Show Notebook Tools
      Show Show Notebook Tools in the right sidebar
    • Show Table of Contents
      Show Show Table of Contents in the left sidebar