我有一个包含 2 列的 Pandas 数据框:
像这样:
embedding language
[0.1 0.2 0.3] fr
[0.1 0.4 0.4] en
[0.8 0.1 0.1] fr
Run Code Online (Sandbox Code Playgroud)
给定一个起始整数n = 10,对于嵌入列的每个值,我想向上述数据框中添加一列,如下所示:
embedding language feature1 feature2 feature3
[0.1 0.2 0.3] fr 10:0.1 11:0.2 12:0.3
[0.1 0.4 0.4] en 13:0.1 14:0.4 15:0.4
[0.8 0.1 0.1] fr 10:0.8 11:0.1 12:0.1
Run Code Online (Sandbox Code Playgroud)
所以,feature1 = 第一个嵌入值,feature2 = 第二个嵌入值......对于下一种语言,开始特征值 = n+size_of_embedding:。因此,对于每种语言,添加的列数恰好等于 size_of_embedding 数组。对于遇到的每种下一种语言,我们从n+size_of_embedding: 开始。有没有简单的方法来做到这一点?谢谢。
category使用这里和这里的解决方案在我的所有列中填充空值后,我的许多浮点列中都留下了许多空值。我认为一个简单的方法df.fillna(0.0, inplace = True)会起作用,但是,我收到了错误ValueError: fill value must be in categories。我认为此错误仅适用于category类型列。
所以,
我有许多浮动列和许多类别列。我通过添加类别“未知”然后用“未知”填充空值来填充类别列。现在,一个简单的
df.fillna(0.0, inplace = True)
Run Code Online (Sandbox Code Playgroud)
应该有效。但是,事实并非如此。
重现此问题的简单方法如下:
df = pd.DataFrame({"A": ["a"], "B":[np.nan] })
df['A'] = df['A'].astype('category')
df.fillna(0.0, inplace = True)
Run Code Online (Sandbox Code Playgroud)
请不要说我能做到:
df['A'].fillna(0.0, inplace = True)
Run Code Online (Sandbox Code Playgroud)
我有很多浮动列,我不能一一去。我必须批量填充剩余列中的所有空值 0.0。请放心,所有列都是浮点类型,但是,可能还有额外的category列,但是,它们没有任何空值。
欣赏任何解决方案。
我有一个带有两个字符串列的 Pandas 数据框,我想在空间上拆分它们,如下所示:
df =
A B
0.1 0.5 0.01 ... 0.3 0.1 0.4 ...
Run Code Online (Sandbox Code Playgroud)
我想拆分这两个列并为尽可能多的值形成新列,这会导致拆分。
所以,结果:
df =
A1 A2. A3 ... B1 B2 B3
0.1 0.5 0.01 ... 0.3 0.1 0.4
Run Code Online (Sandbox Code Playgroud)
目前,我正在做:
df = df.join(df['A'].str.split(' ', expand = True)
df = df.join(df['B'].str.split(' ', expand = True)
Run Code Online (Sandbox Code Playgroud)
但是,我收到以下错误:
columns overlap but no suffix specified
Run Code Online (Sandbox Code Playgroud)
这是因为我猜第一次和第二次拆分的列名称重叠?
所以,我的问题是如何通过为多个拆分提供列名或后缀来拆分多个列?
val inputfile = sqlContext.read
.format("com.databricks.spark.csv")
.option("header", "true")
.option("inferSchema", "true")
.option("delimiter", "\t")
.load("data")
inputfile: org.apache.spark.sql.DataFrame = [a: string, b: bigint, c: boolean]
val outputfile = inputfile.groupBy($"a",$"b").max($"c")
Run Code Online (Sandbox Code Playgroud)
上面的代码失败,因为它c是一个布尔变量,并且聚合不能应用于布尔值。是否有火花功能转换true价值来1并false给0了星火数据帧的整列。
我尝试了以下操作(源:如何在Spark SQL的DataFrame中更改列类型?)
val inputfile = sqlContext.read
.format("com.databricks.spark.csv")
.option("header", "true")
.option("inferSchema", "true")
.option("delimiter", "\t")
.load("data")
val tempfile =inputfile.select("a","b","c").withColumn("c",toInt(inputfile("c")))
val outputfile = tempfile.groupBy($"a",$"b").max($"c")
Run Code Online (Sandbox Code Playgroud)
以下问题:为PySpark在DataFrame中从布尔值转换为整数答案的新派生列,但我想要一个专门用于Scala的函数。
感谢任何帮助。