我正在做一个数据可视化任务,在那里我需要获取一个数据集并进行特定的可视化。请考虑关于数据集的以下内容:
因此,我必须读取数据集,将附加“N”的纬度转换为正浮点值,并将“S”作为负浮点值(整个数据以字符串表示)。
类似地,我必须将附加了“E”的经度转换为正浮点值,并将“W”转换为负浮点值。
因为我是Python,Pandas,Numpy的新手,所以我有很多困难来达到同样的目的。到目前为止,我已经能够将字符串格式的纬度和经度转换为浮点格式,并分别去掉“N”、“S”、“E”和“W”字符。但是,在浮点转换之前,我无法弄清楚如何根据字符('N‘、'S’、'E‘、'W')使浮点数为正数或负值。
下面是我到目前为止编写的代码:
import pandas as pd
df = pd.read_csv("Aug-2016-potential-temperature-180x188.txt", skiprows = range(7))
df.columns = ["longitude"]
df = df.longitude.str.split("\t", expand = True)
smaller = df.iloc[::10,:]
print(df.head(10), end = "\n")
print(smaller, end = "\n")
print(df.iloc[1][3], end = "\n")
print(smaller.iloc[2][175], end = "\n")
import numpy as np
import pandas as pd
data = pd.read_csv('~/documents/datasets/viz_a1/Aug-2016-potential-temperature-180x188.txt', skiprows=7)
data.columns = ['longitudes']
data = data['longitudes'].str.split('\t', expand=True)
df = data.iloc[::10,:]
df.head()
# replace 'E' with '' and 'W' with ''
df.loc[0] = df.loc[0].str.replace('E', '').str.replace('W', '')
# convert the longitude values to float values (THIS ONE WORKS)
df.loc[0] = df.loc[0][1:].astype(float)
# replace 'S' with '' and 'N' with ''
df.loc[:][0] = df.loc[:][0].str.replace('S', '').str.replace('N', '')
# convert latitude values into float values (THIS ONE DOES NOT WORK!!)
df.loc[:][0] = df.loc[:][0].astype(float)
# checking if the float values exist
print(df.loc[0][2], ' data-type ', type(df.loc[0][2])) # columns converted into float
print(df.loc[30][0], ' data-type ', type(df.loc[30][0])) # rows not converted into float 怀疑:
经度转换产生了很多警告。如果有人能解释我为什么收到这些警告,以及如何防止这些警告,那就太好了。(同样,我是Python和Pandas的新手!)
可以找到数据集这里
下面是数据集的屏幕截图:

发布于 2018-09-05 15:17:49
我将在read_csv函数中再添加几个参数,以获得数据,其中列是纵向字符串,索引是纬度。数据中的数据现在是栅格数据。
df = pd.read_csv(r'Aug-2016-potential-temperature-180x188.txt',
skiprows=8, delimiter='\t', index_col=0)然后,我将纵向字符串( dataframe的列)转换为使用以下代码浮动:
column_series = pd.Series(df.columns)
df.columns = column_series.apply(lambda x: float(x.replace('E','')) if x.endswith('E') else -float(x.replace('W','')))在转换纬度字符串( dataframe的索引)后,将使用以下代码浮动:
index_series = pd.Series(df.index)
df.index = index_series.apply(lambda x: float(x.replace('N','')) if x.endswith('N') else -float(x.replace('S','')))发布于 2018-09-05 15:27:03
这可能不是最干净的,但您可以将'N‘和'E’替换为"",然后使用np.where替换'S‘和'W',转换为浮动,然后乘以-1。
我以df为例,将此过程应用于第一列。
example = pd.DataFrame({'1':['S35', 'E24', 'N45', 'W66'],
'2': ['E45', 'N78', 'S12', 'W22']})
example
Out[153]:
1 2
0 S35 E45
1 E24 N78
2 N45 S12
3 W66 W22
col = example.loc[:, '1']
col = col.str.replace('N|E', "")
col
Out[156]:
0 S35
1 24
2 45
3 W66
Name: 1, dtype: object
example.loc[:,'1'] = np.where(col.str.contains('W|S'), col.str.replace('W|S', '').astype('float') * -1, col)
example
Out[158]:
1 2
0 -35 E45
1 24 N78
2 45 S12
3 -66 W22https://stackoverflow.com/questions/52187366
复制相似问题