Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中创建可用的“str”对象_Python_String_Pandas_Object - Fatal编程技术网

在Python中创建可用的“str”对象

在Python中创建可用的“str”对象,python,string,pandas,object,Python,String,Pandas,Object,我需要创建一个str对象,以便在更大的代码块中使用 我有一个数据集作为称为testset的数据帧读入 testset = pd.read_csv('my_dataset_path') 然后,我想将testset中的两列(纬度和经度)组合成一个str对象 我希望它看起来像这样: u = u"""Latitude,Longitude 42.357778,-71.059444 39.952222,-75.163889 25.787778,-80.224167 30.267222, -97.76388

我需要创建一个str对象,以便在更大的代码块中使用

我有一个数据集作为称为testset的数据帧读入

testset = pd.read_csv('my_dataset_path')
然后,我想将testset中的两列(纬度和经度)组合成一个str对象

我希望它看起来像这样:

u = u"""Latitude,Longitude
42.357778,-71.059444
39.952222,-75.163889
25.787778,-80.224167
30.267222, -97.763889"""
但当我尝试以下方法时:

Lat = testset['Latitude'].astype(str) #creates a series
Long = testset['Longitude'].astype(str) #creates a series
Lat_str=Lat.str.cat(sep=' ') #coerces a 'str' object
Long_str=Long.str.cat(sep=' ') #coerces a 'str' object
u= Lat_str,Long_str
我只是从两个str对象中得到一个元组


我不想列出最后一个'str'对象u中的每一项,因为这些列有1000多个条目。有没有一种方法可以简化此过程以获得所需的结果?我尝试过其他的变体来形成u,但它从来都不太正确。

因为你使用熊猫,最简单的方法是使用:

输出:

Latitude,Longitude
42.357778,-71.059444
39.952222,-75.163889
25.787778,-80.22416700000001
30.267221999999997,-97.763889
Latitude,Longitude
42.357778,-71.059444
39.952222,-75.163889
25.787778,-80.224167
30.267222,-97.763889
如果要避免使用30.26722199999997,则:

输出:

Latitude,Longitude
42.357778,-71.059444
39.952222,-75.163889
25.787778,-80.22416700000001
30.267221999999997,-97.763889
Latitude,Longitude
42.357778,-71.059444
39.952222,-75.163889
25.787778,-80.224167
30.267222,-97.763889

您是否尝试过压缩这两列并使用列表理解来连接成对的元素,而不是立即生成字符串?您如何获得第二行纬度/经度条目,您能将其放入您的问题中吗?我想你展示了如何到达第一排。是的,就是这样!我正在研究流编辑IO工具,但您的方法正是按照我想要的方式工作的。谢谢你,迈克。