pandas 显示 Excel 表指定数据
时间: 2023-06-03 10:04:48 浏览: 70
可以使用 pandas.read_excel() 方法来读取 Excel 表中的数据,并使用 pandas.DataFrame 来显示指定的数据。可以使用如下代码实现:
```
import pandas as pd
# 读取 Excel 表数据
df = pd.read_excel('path/to/excel_file.xlsx', sheet_name='Sheet1')
# 显示指定数据
print(df.loc[:, ['Column1', 'Column2']])
```
其中,'path/to/excel_file.xlsx' 需要替换为实际 Excel 表的路径,'Sheet1' 需要替换为实际的工作表名称。'Column1' 和 'Column2' 需要替换为实际需要显示的列名。
相关问题
pandas 写入Excel指定表指定列覆盖数据
可以使用pandas的to_excel方法来覆盖Excel指定表指定列的数据。首先需要读取Excel文件,然后再使用to_excel方法将数据写入Excel文件。下面是一个示例代码:
```python
import pandas as pd
# 读取Excel文件
df = pd.read_excel('data.xlsx', sheet_name='Sheet1')
# 修改指定列的数据
df['列名'] = ['新数据1', '新数据2', '新数据3']
# 将修改后的数据覆盖写入Excel文件
writer = pd.ExcelWriter('data.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1', index=False)
writer.save()
```
在这个示例中,首先使用pandas的read_excel方法读取Excel文件,并指定要读取的表名为Sheet1。然后使用df['列名'] = ['新数据1', '新数据2', '新数据3']修改了指定列的数据。最后使用pandas的ExcelWriter方法和to_excel方法将修改后的数据覆盖写入Excel文件。在to_excel方法中,指定了要写入的表名为Sheet1,并设置index=False,表示不写入行索引。
pandas抽出excel中指定的数据
要从Excel中抽取指定的数据,可以使用Python中的pandas库。首先,你需要使用pandas读取Excel文件。假设你的Excel文件名为`data.xlsx`,并且需要抽取的数据在第一个工作表中,你可以使用以下代码:
```python
import pandas as pd
# 读取Excel文件
df = pd.read_excel('data.xlsx', sheet_name=0)
# 抽取指定的数据
selected_data = df.loc[df['column_name'] == 'value']
```
在这个例子中,`column_name`是需要筛选的列名,`value`是需要筛选的值。`selected_data`是一个包含所有符合条件的行的pandas DataFrame对象。你可以根据需要进一步处理或分析这些数据。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)