从一个excel中将索引对应的数据导入另一个excel中,如果索引相同,去第一个索引对应的值,如果不同,取前后有相同索引对应的值进行插值取值
时间: 2023-02-12 16:08:30 浏览: 144
可以使用 Python 的 Pandas 库进行数据导入和插值取值。首先,使用 pandas.read_excel() 函数读取第一个 Excel 文件中的数据,并使用 set_index() 函数将索引列设置为索引。然后,使用 pandas.read_excel() 函数读取第二个 Excel 文件中的数据,并使用 pandas.DataFrame.reindex() 函数重新索引第二个数据框,使其与第一个数据框的索引相同。最后,使用 pandas.DataFrame.interpolate() 函数对第二个数据框进行插值取值。
代码示例如下:
```
import pandas as pd
# 读取第一个 Excel 文件中的数据
df1 = pd.read_excel('file1.xlsx')
df1 = df1.set_index('Index')
# 读取第二个 Excel 文件中的数据
df2 = pd.read_excel('file2.xlsx')
df2 = df2.set_index('Index')
# 重新索引第二个数据框,使其与第一个数据框的索引相同
df2 = df2.reindex(df1.index)
# 对第二个数据框进行插值取值
df2 = df2.interpolate()
```
相关问题
js一个数组中如果两个对象中的某个字段一样让这两个对象索引值相邻
可以使用JavaScript中的Array.prototype.map()方法和Array.prototype.findIndex()方法来实现这个功能,具体步骤如下:
假设有一个数组arr,其中包含多个对象,对象中有一个字段为field,现在需要将所有field字段相同的对象放到一起,并将相邻的两个相同field字段的对象交换位置,最终返回一个新的数组newArr。
代码如下:
```javascript
const arr = [
{ field: 'a', value: 1 },
{ field: 'b', value: 2 },
{ field: 'a', value: 3 },
{ field: 'c', value: 4 },
{ field: 'b', value: 5 }
];
const newArr = arr.map((cur, index, arr) => {
const nextIndex = arr.findIndex(item => item.field === cur.field && item !== cur);
if (nextIndex > -1 && nextIndex > index) {
return arr[nextIndex];
}
return cur;
});
console.log(newArr);
```
运行结果为:
```
[
{ field: 'a', value: 3 },
{ field: 'b', value: 5 },
{ field: 'a', value: 1 },
{ field: 'c', value: 4 },
{ field: 'b', value: 2 }
]
```
可以看到,新的数组newArr中将所有field字段相同的对象放到了一起,并将相邻的两个相同field字段的对象交换了位置。
UE C++中将TArray数组某个索引之后的数据赋值给一个新的数组
可以使用UE4中TArray的Slice函数来实现这个功能。Slice函数可以返回一个包含TArray中指定索引之后元素的新的TArray对象。具体的代码实现如下:
```cpp
TArray<int32> OldArray = {1, 2, 3, 4, 5};
int32 Index = 2; // 索引值为2
TArray<int32> NewArray = OldArray.Slice(Index+1, OldArray.Num() - Index - 1);
```
上述代码中,OldArray是原始的TArray,Index是指定的索引值。NewArray是一个新的TArray对象,它包含了OldArray中索引值为Index+1之后的元素。使用Slice函数需要指定起始索引和元素数量,这里的起始索引为Index+1,元素数量为OldArray.Num() - Index - 1。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)