这就是我使用原始数据类型创建数据框的方法pyspark:
from pyspark.sql.types import StructType, StructField, DoubleType, StringType, IntegerType
fields = [StructField('column1', IntegerType(), True), StructField('column2', IntegerType(), True)]
schema = StructType(fields)
df = spark.createDataFrame([], schema)
values = [tuple([i]) +
tuple([i])
for i in range(3)]
df = spark.createDataFrame(values, schema)
Run Code Online (Sandbox Code Playgroud)
现在,如果我想要第三列包含字典数据,例如:{“1”:1.0,“2”:2.0,“3”:3.0},我该怎么办?我想创建这个数据框:
+--------------------+-----------------+------------------------------+
|column1 |column2 |column3 |
+--------------------+-----------------+------------------------------+
|1 |1 |{"1": 1.0, "2": 1.0, "3": 1.0}|
+--------------------+-----------------+------------------------------+
|2 |2 |{"1": 2.0, "2": 2.0, "3": 2.0}|
+--------------------+-----------------+------------------------------+
|3 |3 |{"1": 3.0, "2": 3.0, "3": 3.0}|
+--------------------+-----------------+------------------------------+
Run Code Online (Sandbox Code Playgroud)
有一个 MapType 似乎很有帮助,但我不知道如何使用它?
假设创建了数据框,如何根据第三列过滤它,给定一个字典来选择具有该字典值的数据框的行?
我试图在C#中使用HTTPClient对象将发布请求发送到API。这是CURL命令:
curl -X POST https://zzz.zzz.zzz/yyy -F Key=abcd -F media=@"audio.aac"
Run Code Online (Sandbox Code Playgroud)
我写了以下代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using System.IO;
using System.Net.Http.Headers;
namespace Speech2Text
{
class Program
{
static void Main(string[] args)
{
curl().Wait();
}
static async Task curl()
{
var client = new HttpClient();
//Create List of KeyValuePairs
List<KeyValuePair<string, string>> bodyProperties = new List<KeyValuePair<string, string>>();
bodyProperties.Add(new KeyValuePair<string, string>("key", "abcd"));
bodyProperties.Add(new KeyValuePair<string, string>("media", "@audio.aac"));
var dataContent = new FormUrlEncodedContent(bodyProperties.ToArray());
HttpResponseMessage response = await client.PostAsync("https://zzz.zzz.zzz/yyy", dataContent);
HttpContent …Run Code Online (Sandbox Code Playgroud) 我有一个数据框,其中有一列 MapType 类型:
df = spark.createDataFrame(
spark._sc.parallelize(
[[{"x": 30.0, "pool": 20.0, "helium": 10.0}, -5],
[{"x": 40.0, "pool": 30.0, "helium": 20.0}, 5]]
),
[
"col1", "col2"
]
)
Run Code Online (Sandbox Code Playgroud)
+------------------------------+-----+
|col1 |col2 |
+------------------------------+-----+
|[x -> 1.0, y -> 2.0, z -> 3.0]| 5.0 |
|[x -> 4.0, y -> 5.0, z -> 6.0]| 5.0 |
+------------------------------+-----+
Run Code Online (Sandbox Code Playgroud)
我在将其写入 CSV 文件时遇到问题。它抱怨 CSV 数据源不支持地图数据类型。有没有办法将“col1”数据转换为字符串数据类型,以便我可以继续写入 CSV 文件?我需要将数据框转换为如下所示:
+------------------------------+-----+
|col1 |col2 |
+------------------------------+-----+
|"x: 1.0, y: 2.0, z: 3.0" | 5.0 |
|"x: 4.0, y: 5.0, …Run Code Online (Sandbox Code Playgroud) 我正在尝试用 python 编写一个脚本来检测任何给定输入音频文件中是否存在简单的警报声音。我解释了我的解决方案,如果有人可以确认它是一个好的解决方案,我将不胜感激。任何其他可以在 python 中实现的解决方案都是值得赞赏的。
我这样做的方法是通过计算两个信号的 FFT(一个是相反的)来计算两个信号的互相关,然后将它们相乘,然后计算结果的 IFFT。然后找到结果的峰值并将其与预先指定的阈值进行比较,以确定是否检测到警报声音。
这是我的代码:
import scipy.fftpack as fftpack
def similarity(template, test):
corr = fftpack.irfft(fftpack.rfft(test , 2 * test.size ) * \
fftpack.rfft(template[:-1] , 2 * template.size ))
return max(abs(corr))
Run Code Online (Sandbox Code Playgroud)
template 和 test 是信号数据的一维列表。rfft 的第二个参数用于填充零以计算 FFT。但是,我不确定应该添加多少个零。另外,在应用 FFT 之前我应该对给定信号进行归一化吗?例如,根据模板信号的峰值对其进行归一化?