pandas中DataFrame新增行及global变量的使用方式

 更新时间:2024年02月02日 09:41:10   作者:追枫萨  
这篇文章主要介绍了pandas中DataFrame新增行及global变量的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

pandas DataFrame新增行及global变量使用

Global变量

在函数体或类外定义的变量,若想在函数体或类中使用,必须先声明使用的是体外global变量

声明格式:global 变量名

import pandas as pd
import numpy as np
import math
 
#global变量
df_result=pd.DataFrame(columns=['LABELS','DISTANCE'])
 
#计算距离
def dist(x):
    # 计算两点经纬度的火星坐标系距离
    global df_result #使用全局变量
    pa=[0,0]
    pb=[0,0]
    pa[0]=x["LNG"]
    pa[1]=x["LAT"]
    pb[0]=x["LNG_LAST"]
    pb[1]=x["LAT_LAST"]
    label=x["LABELS"]
    
    #计算pa、pb之间经纬度距离
    b =math.pi/ 180
    c =math.sin((float(pb[1]) - float(pa[1])) * b / 2)
    d =math.sin((float(pb[0]) - float(pa[0])) * b / 2)
    a = c * c + d * d * math.cos(float(pa[1]) * b) * math.cos(float(pb[1]) * b)
    dis=int(12756274 * math.atan2(math.sqrt(a), math.sqrt(1 - a)))
    tmp=pd.DataFrame({"LABELS":label,
                      "DISTANCE":dis},index=[0])
 
    #向DataFrame对象新增行
    df_result=df_result.append(tmp,ignore_index=True)

geopandas与pandas

geopandas是基于pandas的逻辑开发的能够处理矢量数据的python库(是否能够处理栅格不太确定)那他与pandas的关系如何呢 用一个例子测试一下

1.载入测试数据

import geopandas as gpd 
import matplotlib.pyplot as plt
icosa =gpd.read_file('./icosa.shp')
icosa1 =gpd.read_file('./icosaPoint.shp')

2.测试geopandas的merge.与pandas的merge函数相似

但是有一点区别

pdJoinData =icosa.merge(icosa1,on='global_id')
print(type(pdJoinData))
 
输出:<class 'pandas.core.frame.DataFrame'>

官方文档:

Attribute joins are accomplished using the merge method. In general, it is recommended to use the merge method called from the spatial dataset. With that said, the stand-alone merge function will work if the GeoDataFrame is in the left argument; if a DataFrame is in the left argument and a GeoDataFrame is in the right position, the result will no longer be a GeoDataFrame.

属性连接是使用该merge方法完成的。一般情况下,建议使用merge从空间数据集调用的方法。

话虽如此,merge如果 GeoDataFrame 在left参数中,独立函数将起作用;如果 DataFrame 在left参数中并且 GeoDataFrame 在right位置,结果将不再是 GeoDataFrame。

也就是所这个合并方法如果dataframe调用的,那么返回的是dataframe (pandas的数据结构)如果是GeoDataFrame 调用 并且右参数是dataframe类型那么返回的是GeoDataFrame类型

还有一种情况官方没有说 两个GeoDataFrame调用属性合并merge得到什么?

如上图得到的是pandas的DataFrame 而且里面存储了两个shp的geometry 也就是存储了两个geoserise

 通过构造函数能把这个DataFrame转化为geoDataFrame 需要指定geometry,现在gdf就是geodataframe了。

和普通的相比,它有多列GeoSeries  只有最后一列 geometry代表的空间。

而且不能输出为地理格式 因为geometry不能作为属性输出 但是可以to_csv

gdf = geopandas.GeoDataFrame(pdJoinData, geometry=pdJoinData['geometry_y'])

综上,pandas和geopandas的数据类型是完全一样的 pandas也可以包含GeoSeries 但是geopandas要包含一个默认的geometry 代表地理空间位置

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

最新评论