当前位置: 首页 > article >正文

Pandas 17-行操作和列操作

1. 行操作

选择行

可以使用标签索引(loc)或位置索引(iloc)来选择行。

import pandas as pd  
  
# 创建示例 DataFrame  
df = pd.DataFrame({  
    'A': [1, 2, 3, 4, 5],    'B': [10, 20, 30, 40, 50]})  
  
# 使用标签索引选择行  
selected_row_loc = df.loc[2]  
print("使用标签索引选择行:")  
print(selected_row_loc)  
  
# 使用位置索引选择行  
selected_row_iloc = df.iloc[2]  
print("\n使用位置索引选择行:")  
print(selected_row_iloc)  
添加行

可以使用 locappend 方法添加行。

# 使用 loc 添加行  
df.loc[5] = [6, 60]  
print("\n使用 loc 添加行:")  
print(df)  
  
# 使用 append 方法添加行  
new_row = pd.Series({'A': 7, 'B': 70})  
df = df.append(new_row, ignore_index=True)  
print("\n使用 append 方法添加行:")  
print(df)  
删除行

可以使用 drop 方法删除行。

# 删除行  
df = df.drop(5)  
print("\n删除行:")  
print(df)  

2. 列操作

选择列

可以使用列名直接选择列。

# 选择列  
selected_column = df['A']  
print("\n选择列:")  
print(selected_column)  
添加列

可以使用列名直接添加列。

# 添加列  
df['C'] = [100, 200, 300, 400, 500]  
print("\n添加列:")  
print(df)  
删除列

可以使用 drop 方法删除列。

# 删除列  
df = df.drop('C', axis=1)  
print("\n删除列:")  
print(df)  
重命名列

可以使用 rename 方法重命名列。

# 重命名列  
df = df.rename(columns={'A': 'Column_A', 'B': 'Column_B'})  
print("\n重命名列:")  
print(df)  

3. 结合行和列操作

在实际应用中,行和列操作可以结合使用,以满足更复杂的数据处理需求。

# 选择特定行和列  
selected_data = df.loc[2, 'Column_A']  
print("\n选择特定行和列:")  
print(selected_data)  
  
# 添加新行和新列  
df.loc[5] = [6, 60]  
df['Column_C'] = [100, 200, 300, 400, 500, 600]  
print("\n添加新行和新列:")  
print(df)  
  
# 删除特定行和列  
df = df.drop(5)  
df = df.drop('Column_C', axis=1)  
print("\n删除特定行和列:")  
print(df)  

总结

通过行操作和列操作,可以灵活地对 DataFrame 进行数据处理。以下是一个完整的示例代码,展示了如何结合使用这些方法:

import pandas as pd  
  
# 创建示例 DataFrame  
df = pd.DataFrame({  
    'A': [1, 2, 3, 4, 5],    'B': [10, 20, 30, 40, 50]})  
  
# 使用标签索引选择行  
selected_row_loc = df.loc[2]  
print("使用标签索引选择行:")  
print(selected_row_loc)  
  
# 使用位置索引选择行  
selected_row_iloc = df.iloc[2]  
print("\n使用位置索引选择行:")  
print(selected_row_iloc)  
  
# 使用 loc 添加行  
df.loc[5] = [6, 60]  
print("\n使用 loc 添加行:")  
print(df)  
  
# 使用 append 方法添加行  
new_row = pd.Series({'A': 7, 'B': 70})  
df = df.append(new_row, ignore_index=True)  
print("\n使用 append 方法添加行:")  
print(df)  
  
# 删除行  
df = df.drop(5)  
print("\n删除行:")  
print(df)  
  
# 选择列  
selected_column = df['A']  
print("\n选择列:")  
print(selected_column)  
  
# 添加列  
df['C'] = [100, 200, 300, 400, 500]  
print("\n添加列:")  
print(df)  
  
# 删除列  
df = df.drop('C', axis=1)  
print("\n删除列:")  
print(df)  
  
# 重命名列  
df = df.rename(columns={'A': 'Column_A', 'B': 'Column_B'})  
print("\n重命名列:")  
print(df)  
  
# 选择特定行和列  
selected_data = df.loc[2, 'Column_A']  
print("\n选择特定行和列:")  
print(selected_data)  
  
# 添加新行和新列  
df.loc[5] = [6, 60]  
df['Column_C'] = [100, 200, 300, 400, 500, 600]  
print("\n添加新行和新列:")  
print(df)  
  
# 删除特定行和列  
df = df.drop(5)  
df = df.drop('Column_C', axis=1)  
print("\n删除特定行和列:")  
print(df)  

http://www.kler.cn/news/288921.html

相关文章:

  • JavaWeb - Vue项目
  • 软考高级网络规划设计师含金量高吗?
  • 数据传输安全——混合加解密(国密)
  • 使用安信可Ai-WB2-12F开启wifi与手机通信TCP-IP(AT指令)
  • 在Spring框架中,如何实现依赖注入?请列举几种注入方式。请解释Spring Boot的自动配置特性,并讨论其如何简化Web应用开发。
  • 释放GPU潜能:PyTorch中torch.nn.DataParallel的数据并行实践
  • PhpStorm下调试功能配置
  • 【C语言】结构体新的理解
  • css重置样式表 reset.css 格式化默认css样式
  • JavaWeb基础 -- SpringMVC请求和响应
  • Unity 3D学习资料集合
  • 山东大学OLED透明展示柜案例:科技赋能,创新展示新体验
  • 使用HTTP代理注意的点
  • shell脚本发送随机请求
  • 计算机视觉之 GSoP 注意力模块
  • 《第二十六章 IO 流 - 字节流》
  • 在项目中使用 redis存储 数据,提高 项目运行速度
  • 【Linux】 理解 Linux 中的 `dup2` 函数
  • Spring框架中的@EventListener注解浅谈
  • 【C++ Primer Plus习题】8.2
  • 直播路由器的原理是什么
  • Linux CentOS 7.39 安装mysql8
  • rabbitmq发送的消息接收不到
  • 告别文档处理烦恼,PDF Guru Anki一键搞定所有
  • 多目标应用:基于双存档模型的多模态多目标进化算法(MMOHEA)的移动机器人路径规划研究(提供MATLAB代码)
  • C语言之猜数字小游戏
  • 【苍穹外卖】Day3 菜品接口
  • dinput8.dll错误应该如何修复呢?五种快速修复dinput8.dll错误的问题
  • SpringBoot开发——初步了解SpringBoot
  • CephX 认证机制及用户管理