None of [Int64Index([2, 1], dtype='int64', name='年级')] are in the [columns]"
时间: 2023-10-31 11:05:40 浏览: 16
这个错误提示通常是由于在 Pandas 的 DataFrame 中使用了不存在的列名或者行索引名称,导致无法进行相关操作。具体来说,这个错误提示表示在某个 DataFrame 中,想要使用的行索引名称或列名不存在,从而导致操作失败。
例如,如果你有一个 DataFrame,其中的行索引名称为 `年级`,但是你在某个操作中使用的是 `grade`,那么就会出现这个错误提示。
可能的解决方案包括:
1. 检查代码中使用的行索引名称或列名是否正确,确保它们存在于 DataFrame 中。
2. 如果 DataFrame 中存在多级行索引或列名,需要使用元组来指定它们,例如 `df.loc[('A', 'B'), ('C', 'D')]`。
3. 如果 DataFrame 中存在空的行索引名称或列名,需要使用 `None` 或空字符串来指定它们,例如 `df.columns = ['', 'A', 'B']`。
4. 如果 DataFrame 中存在重复的行索引名称或列名,需要先将其去重,例如 `df.columns = pd.unique(df.columns)`。
相关问题
tensorflow2中设置张量
在 TensorFlow 2 中,您可以使用 `tf.constant()` 函数来创建张量。例如,以下代码创建一个张量:
```python
import tensorflow as tf
# 创建一个形状为 [2, 3],元素都为 0 的张量
tensor = tf.constant(0, shape=[2, 3])
print(tensor)
```
输出:
```
tf.Tensor(
[[0 0 0]
[0 0 0]], shape=(2, 3), dtype=int32)
```
您可以使用 `tf.Variable()` 函数来创建一个可变的张量,这个张量的值可以被更改。例如,以下代码创建一个可变的张量:
```python
import tensorflow as tf
# 创建一个形状为 [2, 3],元素都为 0 的可变张量
tensor = tf.Variable(tf.zeros(shape=[2, 3]))
print(tensor)
```
输出:
```
<tf.Variable 'Variable:0' shape=(2, 3) dtype=float32, numpy=
array([[0., 0., 0.],
[0., 0., 0.]], dtype=float32)>
```
您还可以使用其他函数来创建张量,例如 `tf.ones()` 函数可以用来创建所有元素为 1 的张量。具体来说,您可以使用以下函数来创建张量:
- `tf.constant(value, dtype=None, shape=None, name='Const')`:创建一个不可变的张量。
- `tf.Variable(initial_value=None, trainable=None, validate_shape=True, caching_device=None, name=None, variable_def=None, dtype=None, import_scope=None, constraint=None)`:创建一个可变的张量。
- `tf.ones(shape, dtype=tf.float32, name=None)`:创建所有元素为 1 的张量。
- `tf.zeros(shape, dtype=tf.float32, name=None)`:创建所有元素为 0 的张量。
- `tf.eye(num_rows, num_columns=None, dtype=tf.float32, name=None)`:创建一个单位矩阵。
除此之外,还有很多其他的函数可以用来创建张量,具体可以参考 TensorFlow 2 的文档。
python读取佛山的excel表格,历遍所有sheet,如果列名不是叫'区','省','街道', id ,'日期','数量', '国家' ,或者最后一行第一列之后的数据,数值低于90则删除这列数据,将处理好的数据保存到原表格
import pandas as pd
# 读取Excel文件
file_path = 'Foshan.xlsx'
df = pd.read_excel(file_path, sheet_name=None)
# 遍历所有sheet
for sheet_name, sheet_data in df.items():
# 获取列名
columns = sheet_data.columns.tolist()
# 判断列名是否符合条件
filter_columns = ['区', '省', '街道', 'id', '日期', '数量', '国家']
valid_columns = [col for col in columns if col not in filter_columns]
# 删除数值低于90的列
for col in valid_columns:
if sheet_data[col].dtype == 'int64' or sheet_data[col].dtype == 'float64':
if sheet_data[col].min() < 90:
sheet_data.drop(columns=[col], inplace=True)
# 保存处理后的数据到原表格
writer = pd.ExcelWriter(file_path, engine='xlsxwriter')
sheet_data.to_excel(writer, sheet_name=sheet_name, index=False)
writer.save()
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)