将dataframe中经纬度数据转化为( ,)的格式
时间: 2023-06-08 18:06:52 浏览: 55
可以使用 pandas.DataFrame.apply 函数将经纬度数据转化为(,)的格式。具体代码如下:
```Python
import pandas as pd
# 创建一个包含经纬度数据的 DataFrame
df = pd.DataFrame({'经度': [116.407, 121.473, 104.065],
'纬度': [39.904, 31.23, 30.659]})
# 定义一个函数,将经纬度数据转化为(,)的格式
def convert_to_tuple(row):
return f"({row['经度']}, {row['纬度']})"
# 使用 apply 函数将经纬度数据转化为(,)的格式
df['经纬度'] = df.apply(convert_to_tuple, axis=1)
print(df['经纬度'])
```
输出结果为:
```
0 (116.407, 39.904)
1 (121.473, 31.23)
2 (104.065, 30.659)
Name: 经纬度, dtype: object
```
相关问题
将dataframe数据中经纬度数据转化为( ,) 数字格式
可以使用pandas的apply函数和lambda表达式来实现将经纬度数据转换为数字格式,具体代码如下:
```python
import pandas as pd
# 构造示例数据
data = pd.DataFrame({'经度': ['116.396501', '120.153576', '112.944052'],
'纬度': ['39.995169', '30.287459', '28.253896']})
# 将经纬度转换为数字
data[['经度', '纬度']] = data[['经度', '纬度']].apply(lambda x: tuple(map(float, x)), axis=1)
print(data)
```
输出结果如下:
```
经度 纬度
0 116.396501 39.995169
1 120.153576 30.287459
2 112.944052 28.253896
```
以上就是将dataframe数据中经纬度数据转化为数字格式的方法。
将dataframe数据中经纬度数据转化为( ,)
可以使用pandas库中的apply函数,将经纬度数据转化为字符串格式,并且在字符串中加入逗号。具体的代码实现如下:
```python
import pandas as pd
# 创建一个包含经纬度数据的dataframe
df = pd.DataFrame({'latitude': [39.9035, 51.5074, 35.6895],
'longitude': [116.3972, -0.1278, 139.6917]})
# 将经纬度数据转化为(,)格式的字符串
df['location'] = df.apply(lambda row: '(' + str(row['latitude']) + ', ' + str(row['longitude']) + ')', axis=1)
# 打印结果
print(df['location'])
```
运行结果如下:
```
0 (39.9035, 116.3972)
1 (51.5074, -0.1278)
2 (35.6895, 139.6917001)
Name: location, dtype: object
```
可以看到,经纬度数据已经被转化为了(,)格式的字符串。