我有一个带有Lat和Long列(float(的数据帧。我使用的库要求lat,long输入的形式为((lat,long((,其中lat,leng是至少有2个元素的元组中的元组的第一个元素。即
coords = ((37.275561,-121.964134),)
type(coords)
> tuple
type(coords[0])
>tuple
我在这类调用中尝试了ast literal_eval和zip:
df.assign(coords=[*zip(df.Lat, df.Lon)])
df['coords'] = df['Lat'].map(str) + ',' + df['Lon'].map(str)
literal_eval(df['coords'][0])
这两种方法都创建了一个2元素元组
t = literal_eval(df['coords'][0])
(37.275561, -121.964134)
t[0] = 37.275561 <----What both of the above calls make.
t[0] = (37.275561, -121.964134) <--- WHAT I Want it to be.
理想情况下,我只能对数据帧调用一些东西。。。
您想要获得以下其中一个(我使用了示例数据(:
import pandas as pd
df = pd.DataFrame({"Lat": range(1,10,2), "Lon": range(10, 15)})
# 1st option
df["coords"]=df[["Lat", "Lon"]].agg(tuple, axis=1)
# 2nd option
df["coords2"]=df[["Lat", "Lon"]].agg(lambda x: (tuple(x),), axis=1)
输出:
Lat Lon coords coords2
0 1 10 (1, 10) ((1, 10),)
1 3 11 (3, 11) ((3, 11),)
2 5 12 (5, 12) ((5, 12),)
3 7 13 (7, 13) ((7, 13),)
4 9 14 (9, 14) ((9, 14),)
使用pandas.DataFrame.itertuples
和list
理解:
df = pd.DataFrame(np.random.random((10, 2)), columns=["Lat", "Lon"])
[(x, ) for x in df.itertuples(False, None)]
输出:
[((0.32427121647946533, 0.027607442056747478),),
((0.7978575521459672, 0.10246067888630661),),
((0.7604534847597701, 0.22688737398122827),),
((0.7772166981423476, 0.4517511790606119),),
((0.05758468082099388, 0.5715157313903072),),
((0.24301536293666137, 0.15681071972461658),),
((0.7856579500084773, 0.7396643931774448),),
((0.529357848080448, 0.02914409356782033),),
((0.7636110612200776, 0.9182403706928705),),
((0.14171738807504053, 0.17194731426254195),)]