草庐IT

pyhton_Pandas

全部标签

python - 类型注释 Pandas DataFrame

如果函数或方法返回PandasDataFrame,您如何记录列名和列类型?有没有办法在Python的内置类型注释中执行此操作,还是您只使用文档字符串?如果您只使用文档字符串,您如何格式化它们以尽可能简洁? 最佳答案 文档字符串格式我使用numpydocstring公约作为基础。如果函数的输入参数或返回参数是具有预定列的pandas数据框,那么我将添加一个reStructuredText样式table带有参数描述的列描述。例如:defrandom_dataframe(no_rows):"""Returndataframewithran

python - sql select group by a having count(1) > 1 equivalent in python pandas?

我很难过滤pandas中的groupby项。我想做selectemail,count(1)ascntfromcustomersgroupbyemailhavingcount(email)>1orderbycntdesc我做到了customers.groupby('Email')['CustomerID'].size()它正确地给出了电子邮件列表及其各自的计数,但我无法实现havingcount(email)>1部分。email_cnt[email_cnt.size>1]返回1email_cnt=customers.groupby('Email')email_dup=email_cnt.

python - 没有日期时间索引的 pandas dataframe 每天重新采样

我在以下形式的pandas中有一个数据框:timestampslight72004-02-2800:58:45150.88262004-02-2800:59:45143.52342004-02-2801:00:45150.88422004-02-2801:01:15150.88592004-02-2801:02:15150.88这里注意索引不是timestamps列。但我想重新采样(或以某种方式对数据进行分类)以反射(reflect)每分钟、每小时、每天等光柱的平均值。我研究了pandas提供的resample方法,它需要数据帧具有该方法工作的数据时间索引(除非我误解了这一点)。所以我

python - 使用 matplotlib colormap 和 pandas dataframe.plot 函数

我正在尝试将matplotlib.colormap对象与pandas.plot函数结合使用:importpandasaspdimportmatplotlib.pyplotaspltimportmatplotlib.cmascmdf=pd.DataFrame({'days':[172,200,400,600]})cmap=cm.get_cmap('RdYlGn')df['days'].plot(kind='barh',colormap=cmap)plt.show()我知道我应该以某种方式告诉颜色图它被馈送的值的范围,但是我不知道在使用pandas.p​​lot()函数时该怎么做,因为这个

python - Pandas:转换独立列中的列值

我有如下所示的PandasDataFrame(df_olymic)。我希望将Type列的值转换为独立的列(df_olympic_table)原始数据框In[3]:df_olympicOut[3]:CountryTypeNum0USAGold461USASilver372USABronze383GBGold274GBSilver235GBBronze176ChinaGold267ChinaSilver188ChinaBronze269RussiaGold1910RussiaSilver1811RussiaBronze19转换后的数据框In[5]:df_olympic_tableOut[5

python - Pandas.DataFrame.rename 方法中的参数 "index"是什么?

PandasDataFrame有一个重命名方法,它接受一个名为“index”的参数。看不懂文档中对参数的描述:DataFrame.rename具体来说,我像文档网页上的示例一样使用它:df.rename(index=str,columns={"A":"a","B":"c"})我理解结果,但我不明白为什么我们设置index=str。index参数有什么用?为什么示例设置index=str? 最佳答案 index参数用于重命名索引,以df为例:df.index#RangeIndex(start=0,stop=3,step=1)df.re

python - 如何从 pandas 数据框创建词袋

这是我的数据框CATEGORYBRAND0NoodleAnakMas1NoodleAnakMas2NoodleIndomie3NoodleIndomie4NoodleIndomie23NoodleIndomie24NoodleMiTelorCap325NoodleMiTelorCap326NoodlePopMie27NoodlePopMie...我已经确定了df类型是string,我的代码是df=data[['CATEGORY','BRAND']].astype(str)importcollections,retexts=dfbagsofwords=[collections.Count

python - Pandas 数据透视表手动对列进行排序

这个问题在这里已经有了答案:HowtochangetheorderofDataFramecolumns?(41个回答)SelectingmultiplecolumnsinaPandasdataframe(22个答案)Sortingcolumnsinpandasdataframebasedoncolumnname[duplicate](11个答案)关闭4年前。对于给定的数据框:UUTtestatestbtestctestdDateTime2017-11-2118:47:291.01.01.03.02017-11-2118:47:301.02.01.04.02017-11-2118:47:

python - Pandas read_csv 在更改列数的情况下添加标题名称

我有很多csv文件,我想用Pandas(pd.read_csv)阅读,但是,在某些文件中,中间添加了一列没有标题,如下例所示:Apples,Pears1,23,45,6,7如果使用pd.read_csv(example_file),则会抛出以下错误“ParserError:错误标记数据。C错误:第4行中预期有2个字段,看到3”我想避免跳过该行,而只是添加一个虚拟header名称,如Unknown1,并得到以下结果:Apples,Pears,Unknown11,2,np.nan3,4,np.nan5,6,7 最佳答案 pandas需要

python - 在 Pandas 中读取带有逗号和字符的 CSV 文件时出现问题

我正在尝试使用pandas读取一个csv文件,该文件有一个名为Tags的列,该列由用户提供的标签组成,并具有诸如-、""、''、1950年代、16世纪之类的标签。由于这些是用户提供的,因此也有许多错误输入的特殊字符。问题是我无法使用pandasread_csv打开csv文件。它显示错误:Cparser,错误标记数据。有人可以帮我将csv文件读入pandas吗? 最佳答案 好的。从我们无法读取的格式错误的CSV文件开始:>>>!catunquoted.csv1950's,xyz.nl/user_003,bad,12317th,red,