df["row2", c("col1", "col3")]
时间: 2024-06-03 08:06:16 浏览: 7
`df["row2", c("col1", "col3")]` 是针对一个数据框 `df` 的索引操作,它会返回数据框中第二行、第一列和第三列所对应的数据。其中,`row2` 表示选择第二行,`c("col1", "col3")` 表示选择第一列和第三列。该操作可以用于选择数据框中任意行和任意列的交叉部分数据。
注意,在使用该操作时,需要确保索引的行名和列名在数据框中存在,否则会返回错误。此外,该操作还支持使用整数下标选择行和列,例如 `df[2, c(1, 3)]` 将返回第二行、第一列和第三列所对应的数据。
相关问题
pandas .loc[row_indexer,col_indexer]
pandas 的 .loc 属性用于选择 DataFrame 中的行和列。row_indexer 参数指定要选择的行的索引,col_indexer 参数指定要选择的列的索引。例如:
```
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
# 选择第 1 行,第 'A' 列和第 'C' 列
df.loc[1, ['A', 'C']]
```
输出:
```
A 2
C 8
Name: 1, dtype: int64
```
import numpy as np import pandas as pd import openpyxl from openpyxl.styles import numbers df_csv = pd.read_csv(r'Permance_a.csv', index_col=0, encoding='utf-8') df_csv.to_excel(r'Permance_a.xlsx') # 源表格和目标表格的文件名 source_file = u'Permance_a.xlsx' target_file = u'Permance2.xlsx' # 要复制的单元格位置 source_start_row = 2 # 开始行 source_start_col = 5 # 开始列 source_end_row = 11 # 结束行 source_end_col = 8 # 结束列 # 要覆盖的单元格位置 target_start_row = 7 # 开始行 target_start_col = 3 # 开始列 target_end_row = 16 # 结束行 target_end_col = 6 # 结束列 # 打开两个工作簿 wb1 = openpyxl.load_workbook(source_file) ws1 = wb1.active wb2 = openpyxl.load_workbook(target_file) ws2 = wb2.active # 将源表格的数据覆盖到目标表格中 for i in range(source_start_row, source_end_row + 1): for j in range(source_start_col, source_end_col + 1): cell1 = ws1.cell(row=i, column=j) cell2 = ws2.cell(row=i + target_start_row - source_start_row, column=j + target_start_col - source_start_col) cell2.value = cell1.value # 保存目标表格 wb2.save(target_file) 生成目标文件的单元格显示:此单元格中的数字为文本格式,或者其前面有撇号,修改以上代码解决该问题
可以在将数据覆盖到目标表格之前,先将目标表格中相关单元格的格式设置为数字格式,可以使用openpyxl.styles中的numbers模块来实现。
修改后的代码如下:
```
import numpy as np
import pandas as pd
import openpyxl
from openpyxl.styles import numbers
df_csv = pd.read_csv(r'Permance_a.csv', index_col=0, encoding='utf-8')
df_csv.to_excel(r'Permance_a.xlsx')
# 源表格和目标表格的文件名
source_file = u'Permance_a.xlsx'
target_file = u'Permance2.xlsx'
# 要复制的单元格位置
source_start_row = 2 # 开始行
source_start_col = 5 # 开始列
source_end_row = 11 # 结束行
source_end_col = 8 # 结束列
# 要覆盖的单元格位置
target_start_row = 7 # 开始行
target_start_col = 3 # 开始列
target_end_row = 16 # 结束行
target_end_col = 6 # 结束列
# 打开两个工作簿
wb1 = openpyxl.load_workbook(source_file)
ws1 = wb1.active
wb2 = openpyxl.load_workbook(target_file)
ws2 = wb2.active
# 将目标表格中相关单元格的格式设置为数字格式
for i in range(target_start_row, target_end_row + 1):
for j in range(target_start_col, target_end_col + 1):
cell = ws2.cell(row=i, column=j)
cell.number_format = numbers.FORMAT_NUMBER
# 将源表格的数据覆盖到目标表格中
for i in range(source_start_row, source_end_row + 1):
for j in range(source_start_col, source_end_col + 1):
cell1 = ws1.cell(row=i, column=j)
cell2 = ws2.cell(row=i + target_start_row - source_start_row, column=j + target_start_col - source_start_col)
cell2.value = cell1.value
# 保存目标表格
wb2.save(target_file)
```
这样就可以保证生成的目标文件中的单元格都是数字格式了。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)