简体   繁体   English

如何让python代码接受任何csv列名

[英]how to make python code accept any csv column name

i want my code to identify csv columns with specific numbers like coordinate number(3°12'45.5") and split the coordinate number i mean i want the code to accept any csv column name. in the code below, the code can only accept column name("lat","long") being assigned to the csv file.if i decide to read another csv file with different column name, i have to change the column name in the code. my aim is I dont want to change column name in the code despite changing the csv file. this is the sample of the table in the csv file below我希望我的代码能够识别具有特定数字的 csv 列,例如坐标编号 (3°12'45.5") 并拆分坐标编号我的意思是我希望代码接受任何 csv 列名称。在下面的代码中,代码只能接受列名(“lat”,“long”)被分配给 csv 文件。如果我决定读取另一个具有不同列名的 csv 文件,我必须更改代码中的列名。我的目标是我不想更改尽管更改了 csv 文件,但代码中的列名。这是下面 csv 文件中表格的示例

lat           long
3°34'45.5"N   8°18'45.5"E
3°56'60.5"N   8°12'72.5"E
3°12'45.5"N   8°12'45.5"E
3°03'55.5"N   7°16'45.5"E

The code I have so far is:我到目前为止的代码是:

import pandas as pd

read= pd.read_csv(r"C:\Users\Temmy\Documents\COORD.csv", sep=",",
              engine="python",names=None)

#### csv column name is called "lat"#####
latt=pd.DataFrame(read['lat'].str.split(r'[°\'"]',3).tolist(),columns= ['degree','m','s','t'])
df=latt.drop('t',1)
df=df.apply(pd.to_numeric)
divide= df/[1,60,3600]
df["latitude"]=divide.sum(axis=1)
print(df["latitude"])

#### csv column name is called "long"#####
longg=pd.DataFrame(read['long'].str.split(r'[°\'"]',3).tolist(),columns= ['degree','m','s','t'])
df1=longg.drop('t',1)
df1=df1.apply(pd.to_numeric)
divide= df1/[1,60,3600]
df1["longitude"]=divide.sum(axis=1)
print(df1["longitude"])

mergee= pd.concat([df1["longitude"],df["latitude"]], axis=1)
print(mergee)

If I understood your problem correctly this should help:如果我正确理解您的问题,这应该会有所帮助:

pd.read_csv(r"C:\Users\Temmy\Documents\COORD.csv", skiprows=1, names=['lat', 'long'])

Here you can read more about read_csv parameters: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html在这里您可以阅读有关read_csv参数的更多信息: https : read_csv

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使 Python LightGBM 代码接受列表 - How to make Python LightGBM code to accept list Python:如何获取多个.csv文件的列的第一个值及其名称,并使用它们创建一个新文件 - Python: How to take the first value of a column of multiple .csv files + its name and make a new file with them python csv 添加列名 - python csv add column name 如何串联目录中的所有CSV,并使用Python将CSV名称添加为列 - How to concatenate all CSVs in a directory, adding CSV name as a column with Python 如何使用 python 根据 csv 中的列名写入 header - How to write to a header based on column name in csv with python 如何使用python中的panda库读取名称中有空格的CSV列 - How to read a CSV Column with space in name using panda library in python 如何将csv文件的一列与python中的图像名称进行比较? - How to compare a column of a csv file with an image name in python? 如何使python的argparse接受任意数量的[-R ab],并将其聚合到列表中 - How can I make python's argparse accept any number of [-R a b]s, and aggregate them into a list 在 python 类型提示中,如何让参数接受基类的任何子类? - In python type-hinting, how can I make an argument accept any subclass of a base class? 如何使聚合的 CSV 文件列在 python 中未聚合? - How to make aggregated CSV file column unaggregated in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM