我使用此代码从字符串类型的日期返回日期名称:
import Pandas as pd
df = pd.Timestamp("2019-04-10")
print(df.weekday_name)
Run Code Online (Sandbox Code Playgroud)
所以当我有“2019-04-10”时,代码返回“星期三”
我想将它应用于 Pyspark DataFrame 中的一列以获取文本中的日期名称。但它似乎不起作用。
>+-------------+
|Reported Date|
+-------------+
| 1/07/2010|
| 1/07/2010|
| 1/07/2010|
| 1/07/2010|
| 1/07/2010|
| 1/07/2010|
| 1/07/2010|
+-------------+
Run Code Online (Sandbox Code Playgroud)
我试图这样做:
sparkDF.withColumn("day",weekday_name(pd.Timestamp('Reported Date')))
Run Code Online (Sandbox Code Playgroud)
但我收到错误消息:NameError: name 'weekday_name' is not defined
谁能帮我这个?谢谢
我搜索了我的问题,发现这个问题与我的问题不同。
我有两个地理数据框,一个包含房屋位置points(~700 点),另一个包含suburbs names它们的位置polygon(~2973 个多边形)。我想将每个点链接到一个多边形,以将每个房屋分配给正确的郊区。
我的地理数据框示例
import geopandas as gpd
from shapely.geometry import Point
from shapely.geometry.polygon import Polygon
#creating geo series
polys = gpd.GeoSeries({
'6672': Polygon([(142.92288, -37.97886,), (141.74552, -35.07202), (141.74748, -35.06367)]),
'6372': Polygon([(148.66850, -37.40622), (148.66883, -37.40609), (148.66920, -37.40605)]),
})
#creating geo dataframe
polysgdf = gpd.GeoDataFrame(geometry=gpd.GeoSeries(polys))
polysgdf
Run Code Online (Sandbox Code Playgroud)
产生以下结果(我的原始地理数据框还包括一个suburb包含郊区名称的列,但我无法将其添加到我的样本中,您只能看到下面的郊区 ID)
geometry
6672 POLYGON ((142.92288 -37.97886, 141.74552 -35.07202, 141.74748 -35.06367, 142.92288 -37.97886))
6372 POLYGON ((148.66850 -37.40622, 148.66883 -37.40609, 148.66920 -37.40605, 148.66850 -37.40622)) …Run Code Online (Sandbox Code Playgroud) 你好,我有一串全名。
string='Christof KochJonathan HarelMoran CerfWolfgang Einhaeuser'
Run Code Online (Sandbox Code Playgroud)
我想按名字和姓氏拆分它以获得这样的输出
['Christof Koch', 'Jonathan Harel', 'Moran Cerf', 'Wolfgang Einhaeuser']
Run Code Online (Sandbox Code Playgroud)
我尝试使用这段代码:
splitted = re.sub('([A-Z][a-z]+)', r' \1', re.sub('([A-Z]+)', r' \1', string))
Run Code Online (Sandbox Code Playgroud)
返回这个结果
['Christof', 'Koch', 'Jonathan', 'Harel', 'Moran', 'Cerf', 'Wolfgang', 'Einhaeuser']
Run Code Online (Sandbox Code Playgroud)
我希望将每个全名作为一个项目。
有什么建议么?谢谢
我正在清理文本,我想删除所有连字符和特殊字符。两个单词之间的连字符除外,例如:tic-tacs, popcorn-flavoured。
我编写了下面的正则表达式,但它删除了每个连字符。
text='popcorn-flavoured---'
new_text=re.sub(r'[^a-zA-Z0-9]+', '',text)
new_text
Run Code Online (Sandbox Code Playgroud)
我希望输出是:
popcorn-flavoured