Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在pandas中获得cume_dist的SQL等价物?_Python_Sql_Pandas_Pyspark_Apache Spark Sql - Fatal编程技术网

Python 如何在pandas中获得cume_dist的SQL等价物?

Python 如何在pandas中获得cume_dist的SQL等价物?,python,sql,pandas,pyspark,apache-spark-sql,Python,Sql,Pandas,Pyspark,Apache Spark Sql,我尝试了不同的pandas方法,如rank、qcut、quantile,但无法获得cume\u dist()的SQL等价物。如何在熊猫中获得以下结果 SQL中解决的完整问题可在此网站上找到: 安装程序 将numpy导入为np 作为pd进口熊猫 df=pd.DataFrame({'name':['Molly','ash','Felix','Smudge','Tigger','Alfie','Oscar','Millie','Misty','Puss','Smokey','Charlie'], ‘品

我尝试了不同的pandas方法,如rank、qcut、quantile,但无法获得
cume\u dist()
的SQL等价物。如何在熊猫中获得以下结果

SQL中解决的完整问题可在此网站上找到:

安装程序
将numpy导入为np
作为pd进口熊猫
df=pd.DataFrame({'name':['Molly','ash','Felix','Smudge','Tigger','Alfie','Oscar','Millie','Misty','Puss','Smokey','Charlie'],
‘品种’:[‘波斯’、‘波斯’、‘英国短毛’、‘英国短毛’、‘暹罗’、‘暹罗’、‘缅因浣熊’、‘缅因浣熊’、‘缅因浣熊’、‘缅因浣熊’、‘英国短毛’],
“重量”:[4.2,4.5,5.0,4.9,3.8,5.5,6.1,5.4,5.7,5.1,6.1,4.8],
“颜色”:[“黑色”、“黑色”、“龟甲”、“黑色”、“龟甲”、“棕色”、“黑色”、“龟甲”、“棕色”、“棕色”、“龟甲”、“棕色”、“黑色”],
‘年龄’:[1,5,2,4,2,5,1,5,2,2,4]})
cume_dist的SQL代码
选择名称、重量、超过(按重量排序)的四分位数(4)作为按重量排序的cats中的重量四分位数
所需的输出(sql给出了这一点,如何在pandas中执行?)
  • 此处给出的SQL实现:
问题:如何在熊猫身上做到这一点? 有没有办法只使用numpy和pandas?

这里是Python(PySpark)版本:

创建spark df 在spark df中使用sql函数
从pyspark.sql.window导入窗口
从pyspark.sql.functions导入cume\u dist
w=Window.orderBy(sdf['weight'])
sdf.选择(“重量”,(重心距离())在(w)*100.上。铸造(
IntegerType()).alias(“百分位”).show()
输出
最后第三个条目Molly的值为80,但应该是83。嗯,好吧,奇怪,我来看看为什么不使用sparksql?如果您正在使用Spark@dsk这不是大数据,我没有使用Spark。Pandas有很多函数,我相信可以用Pandas实现。好的-如果你真的想得到Pandas DF,我建议你用PySpark或Spark SQL完成计算,并将Spark DF转换成Pandas DF:#Spark to Pandas DF_pd=DF.toPandas()#Pandas to Spark df_sp=Spark_session.createDataFrame(df_pd)
name    weight  percent
Tigger  3.8 8
Molly   4.2 17
Ashes   4.5 25
Charlie 4.8 33
Smudge  4.9 42
Felix   5.0 50
Puss    5.1 58
Millie  5.4 67
Alfie   5.5 75
Misty   5.7 83
Oscar   6.1 100
Smokey  6.1 100
import pyspark.sql.functions as F
from pyspark.sql import Window

# Define two windows for cumulating weight
win = Window().orderBy('weight') # rolling sum window
win2 = Window().orderBy(F.lit(1)) # total sum window

# get cumulative distribution
df = df.withColumn('cume_dist', F.sum('weight').over(win)*100./F.sum('weight').over(win2))
schema = StructType([
    StructField('name', StringType(), True),
    StructField('breed', StringType(), True),
    StructField('weight', DoubleType(), True),
    StructField('color', StringType(), True),
    StructField('age', IntegerType(), True),
])

sdf = sqlContext.createDataFrame(df, schema)
sdf.createOrReplaceTempView("cats")
+------+----------+
|weight|percentile|
+------+----------+
|   3.8|         8|
|   4.2|        16|
|   4.5|        25|
|   4.8|        33|
|   4.9|        41|
|   5.0|        50|
|   5.1|        58|
|   5.4|        66|
|   5.5|        75|
|   5.7|        83|
|   6.1|       100|
|   6.1|       100|
+------+----------+