df1.iloc和data有什么区别
时间: 2023-12-16 18:06:17 浏览: 58
`df1.iloc`和`data`都是用于访问和操作数据的Python对象。
`df1.iloc`是Pandas库中DataFrame类型的一个方法,用于按照行号对DataFrame进行索引和切片操作。它是通过行号和列号进行索引的,类似于二维数组的索引方式。例如,`df1.iloc[0:3, 0:2]`表示选取第0行到第2行(不包括第3行)和第0列到第1列(不包括第2列)的数据。
`data`可能是一个变量名,代表一个数据集或数组。这个变量可以是任何形式的数据,比如列表、元组、数组或者Pandas DataFrame。如果`data`是一个Pandas DataFrame,那么我们也可以使用`data.iloc`来按照行号进行索引和切片操作。
因此,`df1.iloc`和`data`之间的区别在于它们所代表的数据类型不同,前者是一个DataFrame对象的方法,后者可能是任何形式的数据对象。
相关问题
headers = ["省", "地市(州)", "县(市、区)", "01-房屋及居民家庭财产损失台账", "如何批量处理excel保存到一个新表中02-农林牧渔业损失台账", "03-工矿商贸业损失台账", "04-基础设施损失台账", "05-公共服务损失台账", "06-其他损失台账", "总损失合计"] new_df1 = pd.DataFrame(columns=headers) # 从原始Sheet表格中读取 A5, B5, C5 单元格的数据,分别填充到新DataFrame的“省”,“地市(州)”,“县(市、区)”列下面。 for df1 in dataframes: new_df1 = new_df1.append( pd.Series(df1.iloc[4, :3].tolist() + [None] * (len(headers) - 3), index=headers), ignore_index=True) # 从原始Sheet表格中读取 D列和E列的数据,逐行扫描,如果D列数据在headers列表中出现,就将该行E列的数据填充到对应的表头headers下面。 for df1 in dataframes: for i in range(4, df1.shape[0]): row_data = df1.iloc[i, :] d_col_data = row_data[3] e_col_data = row_data[4] if d_col_data in headers: new_df1.loc[new_df1.shape[0] - 1, d_col_data] = e_col_data
这是一个Python的代码段,使用了pandas库创建了一个空的DataFrame表格,表格列名为headers中的内容。该表格可以用来存储各种损失台账信息,包括房屋及居民家庭财产损失、农林牧渔业损失、工矿商贸业损失、基础设施损失、公共服务损失、其他损失等,并可以统计总损失合计。您想问的如何批量处理Excel并保存到该表格中,需要具体了解您的情况。
import numpy as np import pandas as pd import matplotlib.pyplot as plt import BPNN from sklearn import metrics from sklearn.metrics import mean_absolute_error from sklearn.metrics import mean_squared_error #导入必要的库 df1=pd.read_excel(r'D:\Users\Desktop\大数据\44.xls',0) df1=df1.iloc[:,:] #进行数据归一化 from sklearn import preprocessing min_max_scaler = preprocessing.MinMaxScaler() df0=min_max_scaler.fit_transform(df1) df = pd.DataFrame(df0, columns=df1.columns) x=df.iloc[:,:4] y=df.iloc[:,-1] #划分训练集测试集 cut=4#取最后cut=30天为测试集 x_train, x_test=x.iloc[4:],x.iloc[:4]#列表的切片操作,X.iloc[0:2400,0:7]即为1-2400行,1-7列 y_train, y_test=y.iloc[4:],y.iloc[:4] x_train, x_test=x_train.values, x_test.values y_train, y_test=y_train.values, y_test.values #神经网络搭建 bp1 = BPNN.BPNNRegression([4, 16, 1]) train_data=[[sx.reshape(4,1),sy.reshape(1,1)] for sx,sy in zip(x_train,y_train)] test_data = [np.reshape(sx,(4,1))for sx in x_test] #神经网络训练 bp1.MSGD(train_data, 1000, len(train_data), 0.2) #神经网络预测 y_predict=bp1.predict(test_data) y_pre = np.array(y_predict) # 列表转数组 y_pre=y_pre.reshape(4,1) y_pre=y_pre[:,0] #画图 #展示在测试集上的表现 draw=pd.concat([pd.DataFrame(y_test),pd.DataFrame(y_pre)],axis=1); draw.iloc[:,0].plot(figsize=(12,6)) draw.iloc[:,1].plot(figsize=(12,6)) plt.legend(('real', 'predict'),loc='upper right',fontsize='15') plt.title("Test Data",fontsize='30') #添加标题 #输出精度指标 print('测试集上的MAE/MSE') print(mean_absolute_error(y_pre, y_test)) print(mean_squared_error(y_pre, y_test) ) mape = np.mean(np.abs((y_pre-y_test)/(y_test)))*100 print('=============mape==============') print(mape,'%') # 画出真实数据和预测数据的对比曲线图 print("R2 = ",metrics.r2_score(y_test, y_pre)) # R2 运行上述程序。在下面这一步中draw=pd.concat([pd.DataFrame(y_test),pd.DataFrame(y_pre)],axis=1);我需要将归一化的数据变成真实值,输出对比图,该怎么修改程序
在将归一化的数据变回真实值的过程中,需要将归一化的因变量(即最后一列)逆转换回来,以得到真实的预测值和真实值。代码如下:
```
# 将预测结果和真实结果逆归一化
y_predict = min_max_scaler.inverse_transform(y_pre.reshape(-1, 1))
y_test = min_max_scaler.inverse_transform(y_test.reshape(-1, 1))
# 画图
draw=pd.concat([pd.DataFrame(y_test), pd.DataFrame(y_predict)], axis=1)
draw.iloc[:,0].plot(figsize=(12,6))
draw.iloc[:,1].plot(figsize=(12,6))
plt.legend(('real', 'predict'), loc='upper right', fontsize='15')
plt.title("Test Data", fontsize='30')
# 输出精度指标
print('测试集上的MAE/MSE')
print(mean_absolute_error(y_predict, y_test))
print(mean_squared_error(y_predict, y_test))
mape = np.mean(np.abs((y_predict-y_test)/(y_test)))*100
print('=============mape==============')
print(mape,'%')
# 输出R2分数
print("R2 = ", metrics.r2_score(y_test, y_predict))
```
在此代码中,我们使用了 MinMaxScaler 对象的 inverse_transform() 方法将归一化的结果转换回原始的数据范围。运行修改后的代码,即可得到真实值与预测值的对比图,以及各种精度指标的输出。
相关推荐
![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)