草庐IT

panda_link

全部标签

python - 替换 Pandas 中跨列的重复值

我有一个简单的数据框:df=[{'col1':'A','col2':'B','col3':'C','col4':'0'},{'col1':'M','col2':'0','col3':'M','col4':'0'},{'col1':'B','col2':'B','col3':'0','col4':'B'},{'col1':'X','col2':'0','col3':'Y','col4':'0'}]df=pd.DataFrame(df)df=df[['col1','col2','col3','col4']]df看起来像这样:|col1|col2|col3|col4||------|----

Python Pandas : error: missing ), 位置 2 处的未终止子模式

我有一个包含字符(((的数据框我想更换。但是在这样做之后我得到了错误:data=[{'Title':'set1((("a","b","c")))'},{'Title':'set2((("d","e","f")))'},{'Title':'set3((("g","h","i")))'},{'Title':'set4((("j","k","l")))'},{'Title':'set5((("m","n","o")))'},{'Title':'set6((("p","q","r")))'}]df=pd.DataFrame(data)df#df['Title']=df['Title'].str

python - Pandas Groupby 一致的水平,即使是空的

我正在尝试使用groupby来创建一个新的数据框,但我需要多索引保持一致。无论子类别是否存在,我都希望按如下方式创建它:importpandasaspddf=pd.DataFrame({'Cat1':['A','A','A','B','B','B','B','C','C','C','C','C','D'],'Cat2':['A','B','A','B','B','B','A','B','B','B','B','B','A'],'Num':[1,1,1,1,1,1,1,1,1,1,1,1,1]})printdf.groupby(['Cat1','Cat2']).sum()输出如下:Num

python - 如何使用 Pandas 将字符串转换回列表

我有一个包含一些数据的txt文件,其中一列是这样的:['BONGO','TOZZO','FALLO','PINCO']为了加载文件,我使用了pandas函数to_csv。加载数据框后,内容看起来没问题,但后来我意识到数据框中的项目不是项目列表,而是一个字符串,其元素是列表中的字符!df['column']返回这样的字符串"['BONGO','TOZZO','FALLO','PINCO']"而不是像这样的列表:['BONGO','TOZZO','FALLO','PINCO']因此,如果我输入df['column'][0],我会得到'['而不是BONGO我应该怎么做才能将字符串转换回其原始

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 - Pandas :用下一个非 NaN/# 连续 NaN 填充 NaN

我正在寻找一个pandas系列并用下一个数值的平均值填充NaN,其中:average=nextnumericalvalue/(#consecutiveNaNs+1)到目前为止,这是我的代码,我只是不知道如何在NaN(以及下一个数值)之间划分filler列编号:importpandasaspddates=pd.date_range(start='1/1/2016',end='1/12/2016',freq='D')nums=[10,12,None,None,39,10,11,None,None,None,None,60]df=pd.DataFrame({'date':dates,'num

python pandas 按一天中的小时求和

我正在使用以下每小时计数(df)的数据集:datframe有8784行(2016年,每小时)。我想看看是否有每日趋势(例如,早上时间是否有所增加。为此,我想创建一个具有一天中的小时(从0到24)的图x轴和y轴上的骑车人数量(类似于下图中来自http://ofdataandscience.blogspot.co.uk/2013/03/capital-bikeshare-time-series-clustering.html的图片)。我尝试了pivot、resample和set_index的不同方法,并使用matplotlib绘制它,但没有成功。换句话说,我找不到一种方法来总结特定时间的每

Windows 上 Visual Studio 2017 中的 Python 包(numpy/pandas/等)

我刚刚安装了带有Python和数据科学工作负载的VisualStudioCommunity。我从Python\MachineLearning模板创建了一个新的回归项目。前几行是:frompandasimportread_tableimportnumpyasnpimportmatplotlib.pyplotasplt首先我得到错误:Nomodulenamedxxx或Missingrequireddependencies[xxx],forpandasornumpy,orscikitlearnorscipy.我原以为它们会作为VisualStudio工作负载的一部分安装,实际上它们似乎在An

python - 如何处理 pandas fillna 中的 `None` 值

我有以下字典:fillna(value={'first_name':'Andrii','last_name':'Furmanets','created_at':None})当我将该字典传递给fillna时,我看到:raiseValueError('mustspecifyafillmethodorvalue')\nValueError:mustspecifyafillmethodorvalue\n"在我看来,它在None值上失败了。我使用的是pandas版本0.20.3。 最佳答案 如果您想使用python的None规范化所有空值。d

python - 将 JSON 读取到 pandas 数据框 - ValueError : Mixing dicts with non-Series may lead to ambiguous ordering

我试图将下面的JSON结构读入pandas数据框,但它抛出了错误消息:ValueError:Mixingdictswithnon-Seriesmayleadtoambiguousordering.Json数据:{"status":{"statuscode":200,"statusmessage":"EverythingOK"},"result":[{"id":22,"club_id":16182},{"id":23,"club_id":16182},{"id":24,"club_id":16182},{"id":25,"club_id":16182},{"id":26,"club_id