pandas追加写入excel_快速介绍Python数据分析库pandas的基础知识和代码示例!

为了能够快速查找和使用功能,使我们在进行机器学习模型时能够达到一定流程化。我创建了这个pandas函数的备忘单。这不是一个全面的列表,但包含了我在构建机器学习模型中最常用的函数。让我们开始吧!

本附注的结构:

  • 导入数据
  • 导出数据
  • 创建测试对象
  • 查看/检查数据
  • 选择查询
  • 数据清理
  • 筛选、排序和分组
  • 统计数据

首先,我们需要导入pandas开始:

import pandas as pd
1

导入数据

使用函数pd.read_csv直接将CSV转换为数据格式。

注意:还有另一个类似的函数pd。read_excel用于excel文件。

# Load data 
df = pd.read_csv('filename.csv') # From a CSV file
df = pd.read_excel('filename.xlsx') # From an Excel file

导出数据

to_csv()将数据存储到本地的文件。我们可以通过df[:10].to_csv()保存前10行。我们还可以使用df.to_excel()保存和写入一个DataFrame到Excel文件或Excel文件中的一个特定表格。

df.to_csv('filename.csv') # Write to a CSV file
df.to_excel('filename.xlsx') # Write to an Excel file

创建测试对象

从输入的数据建立一个DataFrame

# Build data frame from inputted data
df = pd.DataFrame(data = {'Name': ['Bob', 'Sally', 'Scott', 'Katie'],
 'Physics': [68, 74, 77, 78],
 'Chemistry': [84, 100, 73, 90],
 'Algebra': [78, 88, 82, 87]})

eb713160707bf9f013d645e5bbd496df.png

或者从列表中创建一个series

#  Create a series from an iterable my_list
my_list = [['Bob',78],
          ['Sally',91], 
          ['Scott',62],
          ['Katie',78],
          ['John',100]]
df1 = pd.Series(my_list) # Create a series from an iterable my_list

e2886be624c100d4d64ea6ca718ed8d6.png

查看/检查数据

head():显示DataFrame中的前n条记录。我经常把一个数据档案的最上面的记录打印在我的jupyter notebook上,这样当我忘记里面的内容时,我可以回头查阅。

df.head(3) # First 3 rows of the DataFrame

545cbbb2844cf9df8f13112138d0e7b9.png

tail():返回最后n行。这对于快速验证数据非常有用,特别是在排序或附加行之后。

df.tail(3) # Last 3 rows of the DataFrame

f11d9620289903f78584b02033e8e3a1.png

添加或插入行

要向DataFrame追加或添加一行,我们将新行创建为Series并使用append()方法。

在本例中,将新行初始化为python字典,并使用append()方法将该行追加到DataFrame。

在向append()添加python字典类型时,请确保传递ignore_index=True,以便索引值不会被使用。生成的轴将被标记为编号series0,1,…, n-1,当连接的数据使用自动索引信息时,这很有用。

append() 方法的作用是:返回包含新添加行的DataFrame。

#Append row to the dataframe, missing data (np.nan)
new_row = {'Name':'Max', 'Physics':67, 'Chemistry':92, 'Algebra':np.nan}
df = df.append(new_row, ignore_index=True)

ddf01fc261855d29b1ce38b48a38537d.png

向DataFrame添加多行

# List of series  
list_of_series = [pd.Series(['Liz', 83, 77, np.nan], index=df.columns),
                pd.Series(['Sam', np.nan, 94,70], index=df.columns ),
                pd.Series(['Mike', 79,87,90], index=df.columns),
                pd.Series(['Scott', np.nan,87,np.nan], index=df.columns),]
# Pass a list of series to the append() to add multiple rows
df = df.append(list_of_series , ignore_index=True)

078b5c8b7d8687790a7da9bc63975e08.png

我们也可以添加新的列

# Adding a new column to existing DataFrame in Pandas
sex = ['Male','Female','Male','Female','Male','Female','Female','Male','Male']
df['Sex'] = sex

521f53f76e1bf7e6c432edf0af46f695.png

info()函数用于按列获取标题、值的数量和数据类型等一般信息。一个类似但不太有用的函数是df.dtypes只给出列数据类型。

df.info() #Index, Datatype and Memory information

dc4bd7f9e20e97b0c4967d13fecb10eb.png

afb87d649f490788d5aefbbbfcff3857.png
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值