python - 将 pandas 数据帧转换为 xarray 数据集后的大小和顺序更改
问题描述
我正在尝试将数据框导出到 netcdf 文件。据我所知,我可以使用 xarray.Dataset.to_netcdf 函数来做到这一点。因此,我必须将我的数据框转换为 xarray 数据集。这是我正在做的事情:
ypredicted_df = pd.DataFrame(ypredicted, index=ytest.index, columns=ytest.columns.values)
ypredicted_ds = ypredicted_df.to_xarray() # to ds
ypredicted_ds.to_netcdf(os.path.join(output_path, 'ypredicted_wholescene_highres_' + str(max_features) + '.nc'))
ypredicted 是一个ndarray。当我打印 ypredicted_df 和 ypredicted_ds.to_dataframe() 以检查是否有变化时,我看到,订单的那一部分和大小发生了变化:
print(ypredicted_df)
ST_B10
lat lon
50.684918 13.282882 -0.213598
13.283247 0.521064
13.283613 0.162646
13.283978 0.090892
13.284343 -0.060037
... ...
51.397346 13.671611 4.871557
13.671977 4.168761
13.672342 1.421363
13.672708 1.761741
13.673073 2.938208
[5979909 rows x 1 columns]
print(ypredicted_ds.to_dataframe())
ST_B10
lat lon
50.684918 13.282882 -0.213598
13.283247 0.521064
13.283613 0.162646
13.283978 0.090892
13.284343 -0.060037
... ...
51.397346 12.281465 3.387909
12.281099 3.021199
12.280734 2.889664
12.280369 3.197318
12.280003 2.702418
[7441114 rows x 1 columns]
- 数据框的大小不相等
- 最后一行的顺序不同(降序,而第一行升序)
我已经检查过是否包含了一些 nans,但是当我删除 nans 时,大小不会改变。
有人可以解释一下,这里发生了什么吗?为什么从 pandas 数据帧转换为 xarray 数据集后数据帧会有所不同?还有另一种方法可以做到这一点,所以数据框保持不变?或者我可以直接将数据框导出到 netcdf 吗?
谢谢您的帮助 :)
更新:
我再次尝试删除 nans,现在大小相同,但顺序仍然错误。如果这在我绘制它时有一些影响,我现在不知道。
print(ypredicted.to_dataframe().dropna(how='any'))
ST_B10
lat lon
50.684918 13.282882 -0.213598
13.283247 0.521064
13.283613 0.162646
13.283978 0.090892
13.284343 -0.060037
... ...
51.397346 12.281465 3.387909
12.281099 3.021199
12.280734 2.889664
12.280369 3.197318
12.280003 2.702418
[5979909 rows x 1 columns]
但是,为了绘图,我需要一个数据集,因为我还没有找到一种方法来绘制数据框。因此,我仍然需要从数据集中删除 nan。我找到了 xarray.Dataset.dropna,但它还不起作用:
我尝试的第一件事是:
ypredicted_ds.dropna(how='any')
错误信息:
Traceback (most recent call last):
File "script_randomforest_dem.py", line 174, in <module>
output_path_identifier, 3)
File "/lustre/scratch2/ws/1/stwa779b-master/04_workspace/randomforest/randomforest.py", line 104, in randomforest
print(dif_ds.dropna(how='any'))
TypeError: dropna() missing 1 required positional argument: 'dim'
然后我尝试了:
ypredicted_ds.dropna('lon', how='any').to_dataframe()
错误信息:
Empty DataFrame
Columns: [ST_B10]
Index: []
(5979909, 1)
ypredicted_ds.dropna('lat', how='any').to_dataframe()
错误信息:
Empty DataFrame
Columns: [ST_B10]
Index: []
(5979909, 1)
他们都没有工作。当通过 lon 和 lat 删除 nan 时,我可以想象在每个 lon 或 lat 中至少出现一个 nan,因此数据集是空的。现在有人如何使用 xarrays ds.dropna()吗?
我该如何绘图? 作为补充,我试图绘制数据集:
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(10, 7))
ax.imshow(ypredicted_ds['ST_B10'], cmap=cmap)
解决方案
推荐阅读
- python - 将稀疏矩阵拆分为块而不转换为密集矩阵
- angular - 角材料选项卡无法正常工作
- c# - MOQ - 使用相同的参数调用相同的方法两次
- asp.net-mvc - 如何在 MVC 中引用全局虚拟包含?
- java - 俄语字符显示为 ??? 在 Spring-MVC 中
- python - KneighborsClassifier 给出与 linalg.norm 和 scipy.spatial.distance.euclidean 不同的欧几里得值
- linux - 通过删除扩展名后的字符来重命名 Linux 文件
- javascript - 节点 ExpressJS | 如何通过自定义查询参数验证
- php - 我正在寻找有关 WP 错误的建议。WP 更新后,插件页面和 WP-admi 将无法加载。函数 wpdb::prepare() 的参数太少
- python - 如何在 pandas 中选择与字符串列表匹配的行,而不仅仅是一个特定的字符串?