我有4列的星火据帧:location_string,locality,region,和country。我使用谷歌地图的地理编码API来解析每一个location_string,然后在空使用结果来填充locality,region和country领域。
我已将调用地理编码库的函数设为 udf,但我面临的问题是,当我超过 Google API 策略的速率限制时,最终会得到“OVERLIMIT”响应状态。
以下是 Spark 数据帧的示例:
+--------------------------------------------------------------------------------------------------------+------------+------+-------+
|location_string |locality |region|country|
+--------------------------------------------------------------------------------------------------------+------------+------+-------+
|-Tainan City-Tainan, Taiwan |Tainan City |null |TWN |
|093 Cicero, IL |null |null |null |
|1005 US 98 Bypass Suite 7 Columbia, MS 39429 |null |null |null |
|10210 Baltimore Avenue, College Park, MD, US 20740 |College Park|MD |null |
|12 Braintree - Braintree, MA, 02184 |null |null |null | …Run Code Online (Sandbox Code Playgroud) 我想基于特定列"ContactID"对此DataFrame进行分组,但如果组的列"PaymentType"不包含特定值,那么我想从DataFrame中删除整个组.
我有这样的事情:
UniqueID = data.drop_duplicates('ContactID')['ContactID'].tolist()
OnlyRefinance=[]
for i in UniqueID:
splits = data[data['ContactID']==i].reset_index(drop=True)
if any(splits['PaymentType']==160):
OnlyRefinance.append(splits)
OnlyRefinance = pd.concat(OnlyRefinance)
Run Code Online (Sandbox Code Playgroud)
这有效,但它非常慢,我想知道是否有更快的方法来实现这一目标.
我有以下查询用于从分区表中删除行,但它不起作用.用于删除分区表中的行的方法是什么?
delete from SecurityLoan where lender=`SCOTIA, date in inDays, portfolio in portfoliolist
Run Code Online (Sandbox Code Playgroud)
请注意,inDays并portfoliolist在列表
我看到 BigQuery 有一个内置的 I/O 连接器,但我们的很多数据都存储在 Snowflake 中。是否有连接到 Snowflake 的解决方法?我唯一能想到的就是使用 sqlalchemy 运行查询,然后将输出转储到 Cloud Storage Bucket,然后 Apache-Beam 可以从存储在 Bucket 中的文件中获取输入数据。
python pipeline google-cloud-dataflow snowflake-cloud-data-platform apache-beam
我遇到了一个名为 Tabula 的很棒的图书馆,它几乎成功了。不幸的是,第一页上有很多无用的区域,我不希望 Tabula 提取这些区域。根据文档,您可以指定要从中提取的页面区域。但是,无用区域仅位于 PDF 文件的第一页,因此,对于所有后续页面,Tabula 将错过顶部部分。有没有办法指定区域条件仅适用于 PDF 的第一页?
from tabula import read_pdf
df = read_pdf(r"C:\Users\riley\Desktop\Bank Statements\50340.pdf", area=(530,12.75,790.5,561), pages='all')
Run Code Online (Sandbox Code Playgroud) 我想在Python脚本中执行KDB函数.但是,KDB函数包含在单独的Q文件中.那么如何读取此Q文件,然后在Python中的Q文件中查询函数?
我从日期时间原子中提取了年、日、月、小时、分钟和秒。如何在数字位数小于 2 的日、月、时、分、秒中添加前导零?
我有这样的事情:
year:string`year$inDateTime;
day:string`dd$inDateTime;
if[1=(count day);day:("0",day)];
month:string`mm$inDateTime;
if[1=(count month);month:"0",month];
hour:string`hh$inDateTime;
if[1=(count hour);hour:"0",hour];
minute:string`uu$inDateTime;
if[1=(count minute);minute:"0",minute];
second:string`ss$inDateTime;
if[1=(count second);second:"0",second];
Run Code Online (Sandbox Code Playgroud)
但有没有更干净的方法来实现这一点呢?
我需要将 dask 数据帧转换为字典列表作为 API 端点的响应。我知道我可以将 dask 数据帧转换为 pandas,然后从那里我可以转换为字典,但最好将每个分区映射到一个字典,然后连接。
我尝试过的:
df = dd.read_csv(path, usecols=cols)
dd.compute(df.to_dict(orient='records'))
Run Code Online (Sandbox Code Playgroud)
我收到的错误:
AttributeError: 'DataFrame' object has no attribute 'to_dict'
Run Code Online (Sandbox Code Playgroud) 我还是 Beam 的新手,但是您究竟如何从 GCS 存储桶中的 CSV 文件中读取数据?我基本上使用 Beam 将这些文件转换为 Pandas 数据帧,然后应用 sklearn 模型来“训练”这些数据。我见过的大多数示例都预先定义了标题,我希望这个 Beam 管道可以推广到标题肯定不同的任何文件。有一个名为beam_utils的库可以完成我想做的事情,但后来我遇到了这个错误:AttributeError: module 'apache_beam.io.fileio' has no attribute 'CompressionTypes'
代码示例:
import apache_beam as beam
from apache_beam.options.pipeline_options import PipelineOptions
# The error occurs in this import
from beam_utils.sources import CsvFileSource
options = {
'project': 'my-project',
'runner:': 'DirectRunner',
'streaming': False
}
pipeline_options = PipelineOptions(flags=[], **options)
class Printer(beam.DoFn):
def process(self, element):
print(element)
with beam.Pipeline(options=pipeline_options) as p: # Create the Pipeline with the specified options.
data = (p
| …Run Code Online (Sandbox Code Playgroud) 我有一个高度不平衡的 3 个类别的数据集。为了解决这个问题,我sample_weight在 XGBClassifier 中应用了数组,但我没有注意到建模结果有任何变化?分类报告(混淆矩阵)中的所有指标都是相同的。执行上有问题吗?
班级比例:
military: 1171
government: 34852
other: 20869
Run Code Online (Sandbox Code Playgroud)
例子:
pipeline = Pipeline([
('bow', CountVectorizer(analyzer=process_text)), # convert strings to integer counts
('tfidf', TfidfTransformer()), # convert integer counts to weighted TF-IDF scores
('classifier', XGBClassifier(sample_weight=compute_sample_weight(class_weight='balanced', y=y_train))) # train on TF-IDF vectors w/ Naive Bayes classifier
])
Run Code Online (Sandbox Code Playgroud)
数据集样本:
data = pd.DataFrame({'entity_name': ['UNICEF', 'US Military', 'Ryan Miller'],
'class': ['government', 'military', 'other']})
Run Code Online (Sandbox Code Playgroud)
分类报告
python ×8
kdb ×3
apache-beam ×2
pandas ×2
apache-spark ×1
dask ×1
dataframe ×1
date ×1
dictionary ×1
formatting ×1
function ×1
google-api ×1
google-maps ×1
parsing ×1
partition ×1
pdf ×1
pipeline ×1
pyq ×1
pyspark ×1
scikit-learn ×1
snowflake-cloud-data-platform ×1
split ×1
tabula ×1
xgboost ×1
zero ×1