pandas 筛选excel某表头小数点前两位数据
时间: 2023-10-17 13:20:50 浏览: 117
可以使用 pandas 的 str 属性和正则表达式来实现:
```python
import pandas as pd
# 读取 Excel 文件
df = pd.read_excel('example.xlsx')
# 筛选表头小数点前两位数据
pattern = r'^\d{1,2}\.' # 匹配小数点前两位数字
mask = df.columns.str.contains(pattern)
df_filtered = df.loc[:, mask]
# 输出筛选结果
print(df_filtered)
```
其中,`df.columns.str.contains(pattern)` 返回一个布尔型数组,表示每个表头是否匹配正则表达式。使用 `df.loc[:, mask]` 把匹配的表头所在的列筛选出来。
相关问题
pandas 筛选excel小数点前两位
可以使用 pandas 的 round() 方法来将小数点保留到指定位数,然后再进行筛选。
假设有以下 excel 数据:
| | A | B |
|---|------|------|
| 0 | 1.23 | abc |
| 1 | 2.34 | def |
| 2 | 3.45 | ghi |
| 3 | 4.56 | jkl |
代码如下:
import pandas as pd
# 读取 excel 数据
df = pd.read_excel('data.xlsx')
# 将 A 列保留小数点前两位
df['A'] = df['A'].round(2)
# 筛选小数点前两位为 1.23 的行
result = df[df['A'] == 1.23]
print(result)
输出结果为:
A B
0 1.23 abc
可以看到,通过 round() 方法将 A 列的小数点保留到了两位,并且通过筛选找到了小数点前两位为 1.23 的行。
pandas 筛选excel某表头数据
假设你有一个名为 `data.xlsx` 的 Excel 文件,其中有一个名为 `Sheet1` 的工作表。以下是如何使用 Pandas 筛选该表头数据的示例代码:
```python
import pandas as pd
# 读取 Excel 文件
df = pd.read_excel('data.xlsx', sheet_name='Sheet1')
# 筛选某表头数据
header_name = 'Column1'
header_data = df[header_name]
# 打印筛选结果
print(header_data)
```
在上面的代码中,我们首先使用 `pd.read_excel()` 函数读取了 Excel 文件,并将其存储在一个名为 `df` 的 Pandas 数据帧中。然后,我们指定要筛选的表头名称,将其存储在一个名为 `header_name` 的变量中,并使用 `df[header_name]` 语法从 Pandas 数据帧中筛选出该表头数据。最后,我们打印了筛选结果。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.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)