我有一个包含数据读取的CSV文件,我想读入Python.我得到包含字符串的列表"2,5".现在做的float("2,5")不起作用,因为它有错误的小数点.
我如何将其作为Python阅读2.5?
我正在尝试使用列表过滤pyspark中的数据帧.我想要根据列表进行过滤,或者仅包含列表中具有值的记录.我的代码不起作用:
# define a dataframe
rdd = sc.parallelize([(0,1), (0,1), (0,2), (1,2), (1,10), (1,20), (3,18), (3,18), (3,18)])
df = sqlContext.createDataFrame(rdd, ["id", "score"])
# define a list of scores
l = [10,18,20]
# filter out records by scores by list l
records = df.filter(df.score in l)
# expected: (0,1), (0,1), (0,2), (1,2)
# include only records with these scores in list l
records = df.where(df.score in l)
# expected: (1,10), (1,20), (3,18), (3,18), (3,18)
Run Code Online (Sandbox Code Playgroud)
给出以下错误:ValueError:无法将列转换为bool:请使用'&'代表'和','|' 对于'或','〜'表示构建DataFrame布尔表达式时的'not'.
我有一个包含四个字段的数据框.其中一个字段名称是Status,我试图在.filter中使用OR条件来表示数据帧.我试过下面的查询,但没有运气.
df2 = df1.filter(("Status=2") || ("Status =3"))
df2 = df1.filter("Status=2" || "Status =3")
Run Code Online (Sandbox Code Playgroud)
有没有人以前用过这个.我在这里看到了关于堆栈溢出的类似问题.他们使用下面的代码来使用OR条件.但该代码适用于pyspark.
from pyspark.sql.functions import col
numeric_filtered = df.where(
(col('LOW') != 'null') |
(col('NORMAL') != 'null') |
(col('HIGH') != 'null'))
numeric_filtered.show()
Run Code Online (Sandbox Code Playgroud) 我几乎肯定以前曾经问过这个问题,但是通过stackoverflow搜索并没有回答我的问题.不是[2]的重复,因为我想要最大值,而不是最频繁的项目.我是pyspark的新手,并且尝试做一些非常简单的事情:我想将groupBy列"A"分组,然后只保留每个组中具有"B"列中最大值的行.像这样:
df_cleaned = df.groupBy("A").agg(F.max("B"))
Run Code Online (Sandbox Code Playgroud)
不幸的是,这会抛弃所有其他列 - df_cleaned只包含列"A"和B的最大值.我如何保留行?("A","B","C"......)
df(Pandas Dataframe)有三行.
some_col_name
"apple is delicious"
"banana is delicious"
"apple and banana both are delicious"
Run Code Online (Sandbox Code Playgroud)
df.col_name.str.contains("apple|banana")
将捕获所有行:
"apple is delicious",
"banana is delicious",
"apple and banana both are delicious".
Run Code Online (Sandbox Code Playgroud)
如何在str.contains方法上应用AND运算符,以便它只捕获包含苹果和香蕉的字符串?
"apple and banana both are delicious"
Run Code Online (Sandbox Code Playgroud)
我想抓住包含10-20个不同单词的字符串(葡萄,西瓜,浆果,橙子,......等)
我正在使用pySpark,并设置了我的数据框,其中两列代表每日资产价格,如下所示:
ind = sc.parallelize(range(1,5))
prices = sc.parallelize([33.3,31.1,51.2,21.3])
data = ind.zip(prices)
df = sqlCtx.createDataFrame(data,["day","price"])
Run Code Online (Sandbox Code Playgroud)
我开始申请df.show():
+---+-----+
|day|price|
+---+-----+
| 1| 33.3|
| 2| 31.1|
| 3| 51.2|
| 4| 21.3|
+---+-----+
Run Code Online (Sandbox Code Playgroud)
哪个好,一切都好.我想有另一个列,其中包含价格列的日常回报,即类似的内容
(price(day2)-price(day1))/(price(day1))
经过大量研究后,我被告知通过应用这些pyspark.sql.window功能可以最有效地完成,但我无法看到.
我在QC审查中遇到了一些有趣的代码,并对它的行为感到惊讶。我很好奇它是否记录在任何地方。
for i in range(0, my_array.max(), 3)[:]:
# other code here
Run Code Online (Sandbox Code Playgroud)
我对[:]after 的需求感到好奇range,因此我对其进行了测试:
>>> range(0, 10, 3)
range(0, 10, 3)
>>> range(0, 10, 3)[:]
range(0, 12, 3)
Run Code Online (Sandbox Code Playgroud)
这些范围定义的实际顺序是相同的,但是我在Python 范围文档中的任何地方都看不到这种切片行为,因此我很好奇这里的实际情况。
我有一个类似于架构的数据框
root
|-- state: struct (nullable = true)
| |-- fld: integer (nullable = true)
Run Code Online (Sandbox Code Playgroud)
我想在state结构中添加列,即创建一个具有类似架构的数据帧
root
|-- state: struct (nullable = true)
| |-- fld: integer (nullable = true)
| |-- a: integer (nullable = true)
Run Code Online (Sandbox Code Playgroud)
但相反,我得到了
root
|-- state: struct (nullable = true)
| |-- fld: integer (nullable = true)
|-- state.a: integer (nullable = true)
Run Code Online (Sandbox Code Playgroud)
这是尝试
df.withColumn('state.a', val)
Run Code Online (Sandbox Code Playgroud) 我想在这样的时候评估两个条件: -
import pyspark.sql.functions as F
df = df.withColumn(
'trueVal', F.when(df.value < 1 OR df.value2 == 'false' , 0 ).otherwise(df.value))
Run Code Online (Sandbox Code Playgroud)
为此我使用'OR'得到'无效语法'
即使我尝试使用嵌套的when语句: -
df = df.withColumn(
'v',
F.when(df.value < 1,(F.when( df.value =1,0).otherwise(df.value))).otherwise(df.value)
)
Run Code Online (Sandbox Code Playgroud)
为此,我得到'keyword can't be an expression'嵌套的when语句.
我怎么能在when任何工作中使用多个条件?
apache-spark ×6
pyspark ×6
python ×4
dataframe ×2
pyspark-sql ×2
filter ×1
locale ×1
pandas ×1
python-3.x ×1
slice ×1
string ×1