我有一个超过40万行的pandas数据框,现在我想计算每一行的四分位数范围,但是我的代码产生了以下错误:
不能从空轴做非空拿
我的代码:
def calIQR(x):
x=x.dropna()
return (np.percentile(x,75),np.percentile(x,25))
df["count"]=df.iloc[:,2:64].apply(calIQR,axis=1)
Run Code Online (Sandbox Code Playgroud)
我正在运行python 2.7.13
我在网上搜索,但仍然不知道为什么会发生此错误。
在每一行中,都有一些NaN值,但是我确定没有一行会显示所有NaN。
我是Pandas的新手,我有一个如下的数据框
id values
1 2.1
2 0.8
3 1.0
4 3.2
Run Code Online (Sandbox Code Playgroud)
我想将列"值"分成不同的bin,比如bin = 2并添加一列"counts",表示bin中有多少行,例如:
id values counts
1 2.1 2 (since 2.1 and 3.2 both belong to the bin 2-4)
2 0.8 2
3 1.0 2
4 3.2 2
Run Code Online (Sandbox Code Playgroud)
我知道value_counts函数可以计算频率,但我不知道如何将它们追加回原始数据帧.
任何帮助深表感谢!
我有两个数据帧df1和ip2Country.
df1包含IP地址,我试图将IP地址映射到经度和纬度等地理位置信息中.ip2Country
我将它作为Spark提交作业运行,但操作需要很长时间,即使df1只有少于2500行.
我的代码:
val agg =df1.join(ip2Country, ip2Country("network_start_int")=df1("sint")
, "inner")
.select($"src_ip"
,$"country_name".alias("scountry")
,$"iso_3".alias("scode")
,$"longitude".alias("slong")
,$"latitude".alias("slat")
,$"dst_ip",$"dint",$"count")
.filter($"slong".isNotNull)
val agg1 =agg.join(ip2Country, ip2Country("network_start_int")=agg("dint")
, "inner")
.select($"src_ip",$"scountry"
,$"scode",$"slong"
,$"slat",$"dst_ip"
,$"country_name".alias("dcountry")
,$"iso_3".alias("dcode")
,$"longitude".alias("dlong")
,$"latitude".alias("dlat"),$"count")
.filter($"dlong".isNotNull)
有没有其他方式加入这两个表?或者我做错了吗?