草庐IT

sample_dataframe

全部标签

python - 为什么随机抽样与数据集而不是样本量成比例? ( Pandas .sample() 示例)

当我从不同大小的分布中随机抽样时,我惊讶地发现执行时间似乎主要与被抽样的数据集的大小成比例,而不是被抽样的值的数量。示例:importpandasaspdimportnumpyasnpimporttimeastm#generateasmallandalargedatasettestSeriesSmall=pd.Series(np.random.randn(10000))testSeriesLarge=pd.Series(np.random.randn(10000000))sampleSize=10tStart=tm.time()currSample=testSeriesLarge.sa

python - 意外类型 : <class 'pyspark.sql.types.DataTypeSingleton' > when casting to Int on a ApacheSpark Dataframe

尝试将StringType转换为pyspark数据帧上的IntType时出现错误:joint=aggregates.join(df_data_3,aggregates.year==df_data_3.year)joint2=joint.filter(joint.CountyCode==999).filter(joint.CropName=='WOOL')\.select(aggregates.year,'Production')\.withColumn("ProductionTmp",df_data_3.Production.cast(IntegerType))\.drop("Prod

python - 'DataFrame' 对象没有属性 'melt'

我只想在pandas中使用melt函数,但我总是遇到同样的错误。只需键入文档提供的示例:cheese=pd.DataFrame({'first':['John','Mary'],'last':['Doe','Bo'],'height':[5.5,6.0],'weight':[130,150]})我刚得到错误:---------------------------------------------------------------------------AttributeErrorTraceback(mostrecentcalllast)in()---->1cheese.melt(i

python - 获取 `pandas.DataFrame` 中列数总和最大的前 3 行?

这是我的pandas.DataFrame:day1day2day3Apple401398Orange324556Banana567687Pineapple121912Grape894567我想创建一个新的DataFrame,它将包含三天总和最大的前3个水果。apple三天的总和--151,orange--133,banana--219,菠萝--43,葡萄--201.所以排名前三的水果是:1)banana;2)葡萄;3)苹果。这是预期的输出:day1day2day3Banana567687Grape894567Apple401398我如何使用pandas.DataFrame做到这一点?谢

python - 导入 pandas.DataFrame 时获取 "IOError: [Errno 13] Permission denied:.."

我得到了IOError:[Errno13]Permissiondenied:'/usr/local/lib/python2.7/dist-packages/python_dateutil-2.2-py2.7.egg/EGG-INFO/top_level.txt'当我尝试导入Pandas时。我不明白为什么。在python3控制台中导入pandas就可以了。代码的执行也是用Python3完成的start_simulation.pyfromMarketimportMarketfromTestingAlgorithmimportTestingAlgorithmfromLiteForexHand

python - 使用 Pandas 和 PyMongo 将 MongoDB 数据加载到 DataFrame 的更好方法?

我有一个0.7GB的MongoDB数据库,其中包含我试图加载到数据框中的推文。但是,我收到一个错误。MemoryError:我的代码是这样的:cursor=tweets.find()#Wheretweetsismycollectiontweet_fields=['id']result=DataFrame(list(cursor),columns=tweet_fields)我已经尝试了以下答案中的方法,这些方法有时会在加载之前创建数据库所有元素的列表。https://stackoverflow.com/a/17805626/2297475https://stackoverflow.com

python - Pandas - 用空的 python dict 对象替换 DataFrame 中的所有 NaN 值

我有一个pandasDataFrame,其中每个单元格都包含一个python字典。>>>data={'Q':{'X':{2:2010},'Y':{2:2011,3:2009}},'R':{'X':{1:2013}}}>>>frame=DataFrame(data)>>>frameQRX{2:2010}{1:2013}Y{2:2011,3:2009}NaN我想用一个空的字典替换NaN,以获得这个结果:QRX{2:2010}{1:2013}Y{2:2011,3:2009}{}但是,因为fillna函数不是将空字典解释为标量值,而是解释为列-->值的映射,所以如果我只是这样做,它什么也不做(

python - 如何将每行加一的简单计数器列添加到 Pandas DataFrame?

我经常遇到这个问题。如果您在Pandas中有一个现有的DataFrame,并且您想要添加一个只是增加计数的行,即。0、1、2...,最有效的方法是什么?谢谢!山姆 最佳答案 最简单的方法可能是df=df.reset_index()这将为您提供一个从0开始的新索引。你也可以这样做df['counter']=range(len(df)) 关于python-如何将每行加一的简单计数器列添加到PandasDataFrame?,我们在StackOverflow上找到一个类似的问题:

python - 将列添加到包含先前数据平均值的 Pandas DataFrame 的末尾

我有一个包含以下内容的DataFrameave_data:ave_dataTimeF7F8F900:00:0043.005593-56.50974625.27127101:00:0055.114918-59.17385231.84926202:00:0063.990762-64.69949252.426017我想在此数据框中添加另一列,其中包含每行F7、F8和F9列下的平均值。ave_dataDataFrame可能会随着我的代码稍后从不同的Excel文件中读取而改变大小,因此该方法需要是通用的(即添加包含平均值的列始终作为DataFrame中的最后一列,不在第4列中)desiredou

python Pandas : how to find rows in one dataframe but not in another?

假设我有两个表:people_all和people_usa,它们具有相同的结构,因此具有相同的主键。我怎样才能得到不在美国的人的表格?在SQL中,我会做类似的事情:selecta.*frompeople_allaleftouterjoinpeople_usauona.id=u.idwhereu.idisnullPython的等价物是什么?我想不出将这个where语句翻译成pandas语法的方法。我能想到的唯一方法是在people_usa中添加一个任意字段(例如people_usa['dummy']=1),进行左连接,然后只取“dummy”所在的记录'是nan,然后删除虚拟字段-这看起来