草庐IT

sample_dataframe

全部标签

python - pandas dataframe - 根据列标题更改值

我有一个如下所示的dataframe:In[74]:data2Out[74]:abc2012-06-120112012-06-131102012-06-141012012-06-151012012-06-161102012-06-17101有没有办法让值=值=1的列标题?结果df:abc2012-06-120bc2012-06-13ab02012-06-14a0c2012-06-15a0c2012-06-16ab02012-06-17a0c然后删除=0的值,使df减少到2列:(此时列标题不相关)结果df:122012-06-12cb2012-06-13ab2012-06-14ac201

python - Pandas DataFrame 按天/小时/分钟切片

我有带有日期时间索引的pandasDataframe,例如“YYYY-MM-DDHH:MM:SS”。IndexParameter2007-05-0214:14:08134.82007-05-0214:14:32134.82007-05-0214:14:41134.82007-05-0214:14:53134.82007-05-0214:15:01134.82007-05-0214:15:09134.8......2007-05-3023:08:02105.92007-05-3023:18:02105.92007-05-3023:28:02105.92007-05-3023:38:031

python - Pandas - Groupby 并创建新的 DataFrame?

这是我的情况-In[1]:dataOut[1]:ItemType0OrangeEdible,Fruit1BananaEdible,Fruit2TomatoEdible,Vegetable3LaptopNonEdible,ElectronicIn[2]:type(data)Out[2]:pandas.core.frame.DataFrame我想做的是创建一个只有Fruits的数据框,所以我需要groupby这样Fruit存在于类型。我试过这样做:grouped=data.groupby(lambdax:"Fruit"inx,axis=1)我不知道这是否是这样做的方式,我在理解groupb

python - 从 DataFrame 中减去一个 Series,同时保持 DataFrame 结构不变

如何从DataFrame中减去Series,同时保持DataFrame结构完整?df=pd.DataFrame(np.zeros((5,3)))s=pd.Series(np.ones(5))df-s012340-1-1-1NaNNaN1-1-1-1NaNNaN2-1-1-1NaNNaN3-1-1-1NaNNaN4-1-1-1NaNNaN我想要的是相当于从DataFrame中减去一个标量df-10120-1-1-11-1-1-12-1-1-13-1-1-14-1-1-1 最佳答案 也许:>>>df=pd.DataFrame(np.ze

python - 在多索引 pandas DataFrame 上选择一列

给定这个DataFrame:frompandasimportDataFramearrays=[['bar','bar','baz','baz','foo','foo'],['one','two','one','two','one','two']]tuples=zip(*arrays)index=pd.MultiIndex.from_tuples(tuples,names=['first','second'])df=DataFrame(randn(3,6),index=[1,2,3],columns=index)如何绘制图表:X轴:1、2、3。这三个系列的名字是:bar、baz、foo。

python - 推断 Pandas DataFrame

使用Series.interpolate很容易在Pandas.DataFrame中插入值,如何进行外推?例如,给定一个如图所示的DataFrame,我们如何将它外推14个月到2014年12月31日?线性外推法很好。X1=range(10)X2=map(lambdax:x**2,X1)df=pd.DataFrame({'x1':X1,'x2':X2},index=pd.date_range('20130101',periods=10,freq='M'))我认为必须首先创建一个新的DataFrame,DateTimeIndex从2013-11-31开始,再延长14个M时间段。除此之外,我被

python - 使用 pandas dataframe 绘制误差线 matplotlib

我确信这相对容易,但我似乎无法让它发挥作用。我想使用matplotlib模块绘制此df,其中日期为x轴,gas为y轴,std为错误栏。我可以使用pandas包装器让它工作,但我不知道如何设置错误栏的样式。使用Pandasmatplotlib包装器我可以使用matplotlibpandaswrappertrip.plot(yerr='std',ax=ax,marker='D')绘制误差线但是我不确定如何使用plt.errorbar()访问错误栏以像在matplotlib中那样设置它们的样式使用Matplotlibfig,ax=plt.subplots()ax.bar(trip.index

python - 按键更新 pandas DataFrame

我有一个历史股票交易的数据框。该框架包含['ticker'、'date'、'cusip'、'profit'、'security_type']等列。最初:trades['cusip']=np.nantrades['security_type']=np.nan我有历史配置文件,我可以加载到具有['ticker'、'cusip'、'date'、'name'、'security_type'、'primary_exchange']等列的框架中。我想用配置中的cusip和security_type更新交易框架,但仅限于代码和日期匹配的地方。我以为我可以做这样的事情:pd.merge(trades,

python - 如何检查 pandas DataFrame 中的特定单元格是否为空?

我在pandas中有以下df。0ABC12NaN8如何检查df.iloc[1]['B']是否为NaN?我尝试使用df.isnan()并得到了这样的表格:0ABC1falsetruefalse但我不确定如何为表格编制索引,以及这是否是执行工作的有效方式? 最佳答案 使用pd.isnull,供选择使用loc或iloc:print(df)0ABC012NaN8print(df.loc[0,'B'])nana=pd.isnull(df.loc[0,'B'])print(a)Trueprint(df['B'].iloc[0])nana=pd.

python - 在 python pandas 中,如何重新采样和插入 DataFrame?

我有一个pdDataFrame,通常采用这种格式:12340.11000.0000E+001.0000E+005.0000E+000.13237.7444E-058.7935E-011.0452E+000.15454.3548E-047.7209E-014.5432E-010.17681.2130E-036.7193E-012.6896E-010.19902.5349E-035.7904E-011.8439E-010.22134.5260E-034.9407E-011.3771E-01我想做的是从列表中重新采样第1列(索引)值,例如:indexList=numpy.linspace(0