将字符串转换为 DataFrame 中的数字 Pandas
Convert Strings to Numbers in DataFrame Pandas
背景:
我从 Google 地图中获得了一个点列表,将数据提取为 csv。在 Pandas 中对其进行清理,并将其导出为 JSON 文件。 (用于出口的二手记录)
问题:
坐标是字符串。这是有道理的,因为最初,坐标与 url
Example: https://www.google.com/maps/search/{coordinates}
我使用替换功能清除了文本,只保留坐标。有没有办法让我的 Location 列中的值成为数字类型并将它们放入列表中。
示例 我的 导出的 JSON 文件的模型数据如下所示:
[
{
"Bin":"Yes",
"Location":"##.##,-###.##"
},
我试图清理我的数据,使其看起来像下面的示例
示例 GeoJSON 我试图建模的文件
[
{
location: [41.8781, -87.6298],
city: "Chicago"
},
目标:
我正在尝试制作一个自定义地图供我在 mapbox
中使用
示例 我的 DataFrame 的模型
Bin Location
0 Yes ##.##,-###.##
1 Yes ##.##,-###.##
输入:df.types
Output:
Bin object
Location object
dtype: object
感谢您的帮助。
您需要将数字存储在位置中作为单独的列(我假设这些是 lat/long 坐标),以便将它们视为数字并按您期望的方式工作。理想情况下,您应该将 json 清理代码更改为 return 在将其读入数据帧之前看起来像这样的结果:
{
lat: 41.8781,
long: -87.6298,
city: "Chicago"
}
但是你也可以在数据帧中解决这个问题:
json_data = [
{"location": [41.8781, -87.6298], "city": "chicago"},
{"location": [44.8141, 20.1234], "city": "somewhere"}
]
df = pd.DataFrame.from_records(json_data)
print(df)
location city
0 [41.8781, -87.6298] chicago
1 [44.8141, 20.1234] somewhere
print(df.dtypes)
location object
city object
dtype: object
应用我们的转换:
df[["lat", "long"]] = pd.DataFrame(df["location"].tolist(), columns=["lat", "long"])
print(df)
location city lat long
0 [41.8781, -87.6298] chicago 41.8781 -87.6298
1 [44.8141, 20.1234] somewhere 44.8141 20.1234
print(df.dtypes)
location object
city object
lat float64
long float64
dtype: object
我们刚刚所做的是告诉 pandas 我们的“位置”列实际上有 2 个值,它们应该在不同的列中。我们将其展开并将其添加回原始数据框。
如果出于某种原因,pandas 没有自动将您的 lat/long 列解析为浮点数,您可以使用 pd.to_numeric
将对象列转换为 integer/float dtypes。
df["lat"] = pd.to_numeric(df["lat"])
df["long"] = pd.to_numeric(df["long"])
print(df)
location city lat long
0 [41.8781, -87.6298] chicago 41.8781 -87.6298
1 [44.8141, 20.1234] somewhere 44.8141 20.1234
print(df.dtypes)
location object
city object
lat float64
long float64
dtype: object
背景: 我从 Google 地图中获得了一个点列表,将数据提取为 csv。在 Pandas 中对其进行清理,并将其导出为 JSON 文件。 (用于出口的二手记录)
问题: 坐标是字符串。这是有道理的,因为最初,坐标与 url
Example: https://www.google.com/maps/search/{coordinates}
我使用替换功能清除了文本,只保留坐标。有没有办法让我的 Location 列中的值成为数字类型并将它们放入列表中。
示例 我的 导出的 JSON 文件的模型数据如下所示:
[
{
"Bin":"Yes",
"Location":"##.##,-###.##"
},
我试图清理我的数据,使其看起来像下面的示例
示例 GeoJSON 我试图建模的文件
[
{
location: [41.8781, -87.6298],
city: "Chicago"
},
目标: 我正在尝试制作一个自定义地图供我在 mapbox
中使用示例 我的 DataFrame 的模型
Bin Location
0 Yes ##.##,-###.##
1 Yes ##.##,-###.##
输入:df.types
Output:
Bin object
Location object
dtype: object
感谢您的帮助。
您需要将数字存储在位置中作为单独的列(我假设这些是 lat/long 坐标),以便将它们视为数字并按您期望的方式工作。理想情况下,您应该将 json 清理代码更改为 return 在将其读入数据帧之前看起来像这样的结果:
{
lat: 41.8781,
long: -87.6298,
city: "Chicago"
}
但是你也可以在数据帧中解决这个问题:
json_data = [
{"location": [41.8781, -87.6298], "city": "chicago"},
{"location": [44.8141, 20.1234], "city": "somewhere"}
]
df = pd.DataFrame.from_records(json_data)
print(df)
location city
0 [41.8781, -87.6298] chicago
1 [44.8141, 20.1234] somewhere
print(df.dtypes)
location object
city object
dtype: object
应用我们的转换:
df[["lat", "long"]] = pd.DataFrame(df["location"].tolist(), columns=["lat", "long"])
print(df)
location city lat long
0 [41.8781, -87.6298] chicago 41.8781 -87.6298
1 [44.8141, 20.1234] somewhere 44.8141 20.1234
print(df.dtypes)
location object
city object
lat float64
long float64
dtype: object
我们刚刚所做的是告诉 pandas 我们的“位置”列实际上有 2 个值,它们应该在不同的列中。我们将其展开并将其添加回原始数据框。
如果出于某种原因,pandas 没有自动将您的 lat/long 列解析为浮点数,您可以使用 pd.to_numeric
将对象列转换为 integer/float dtypes。
df["lat"] = pd.to_numeric(df["lat"])
df["long"] = pd.to_numeric(df["long"])
print(df)
location city lat long
0 [41.8781, -87.6298] chicago 41.8781 -87.6298
1 [44.8141, 20.1234] somewhere 44.8141 20.1234
print(df.dtypes)
location object
city object
lat float64
long float64
dtype: object