是否有一种方法可以根据字符串拆分snowpark数据框架列?
这是我到目前为止所尝试的
from snowflake.snowpark import Session
from snowflake.snowpark import functions as SF
connection_parameters = {
"account": "myaccount",
"user": "myuser",
}
session = Session.builder.configs(connection_parameters).create()
dh_session = session.table('tableName')
dh = dh_session.select(SF.to_timestamp(SF.col("timestamp")).as_("timestamp"),SF.col("name"))
# Split the name column by string delimiter '-AA' and get the first part
dh.select(SF.split("name",SF.lit("-AA").get_value(0)).as_("test")).show()
但是我得到一个错误信息
AttributeError: 'Column' object has no attribute 'getItem'
感谢不要用get_value
,试试用get
,
from snowflake.snowpark import Session
from snowflake.snowpark import functions as SF
connection_parameters = {
"account": "myaccount",
"user": "myuser",
}
session = Session.builder.configs(connection_parameters).create()
dh_session = session.table('tableName')
dh = dh_session.select(SF.to_timestamp(SF.col("timestamp")).as_("timestamp"),SF.col("name"))
# Split the name column by string delimiter '-AA' and get the first part
dh = dh.select(SF.split("name",SF.lit("-AA")).as_("split_name"))
dh.select(SF.get(dh.split_name, SF.lit(0)).as_("name[0]")).show()
第一个使用过滤器的例子然后选择使用样例数据框。请注意,这使用了一个'like'匹配,它可能是也可能不是一个准确的数据正则表达式。
from snowflake.snowpark import Session
from snowflake.snowpark import functions as SF
connection_parameters = {
"account": "myaccount",
"user": "myuser",
}
session = Session.builder.configs(connection_parameters).create()
##
## Create a dummy dataframe
##
dh = session.create_dataframe([["morning-CC", 2], ["hello-AA", 4],
["bye-BB", 7], ["another-AA", 3]], schema=["name", "col2"])
dh.filter("name like '%-AA%'").select(col("name"),
SF.split(col("name"),SF.lit("-"))[0].alias("test")).show()
<表类>"NAME" "TEST" tbody><<tr>hello-AA "hello" another-AA "another" 表类>