草庐IT

python - 将数据框中某些列的 Float 转换为 Int

我正在尝试将第0列转换为第4列,并将第6列从当前的浮点类型转换为整数。我试过:df[0:4,6].astype(int)但这当然行不通... 最佳答案 考虑dfdf=pd.DataFrame(np.random.rand(10,10)*10)使用np.r_获取slcslc=np.r_[0:4,6]df[slc]=df[slc].astype(int)df或者传递一个以键作为列名的类型字典df.astype({c:intforcinslc}) 关于python-将数据框中某些列的Float

python - 打印包含两列的字符串

我正在尝试打印一个包含两个固定列的字符串。例如,我希望能够打印:abcxyzabcdexyzaxyz打印时格式化输出字符串的正确方法是什么?还有,2.6版本之前和2.6版本之后是怎么做到的? 最佳答案 您可以使用格式并在列之间提及修复空格'{0:10}{1}'.format(s1,s2)旧样式格式'%-10s''%s'%(s1,s2) 关于python-打印包含两列的字符串,我们在StackOverflow上找到一个类似的问题: https://stackov

python - Pandas 数据框列的中位数

我有一个DataFramedf:namecountaaaa2000bbbb1900cccc900dddd500eeee100我想查看与count列的中位数相差10倍以内的行。我尝试了df['count'].median()并得到了中位数。但不知道如何进一步进行。你能建议我如何使用pandas/numpy吗?预期输出:namecountdistancefrommedianaaaa2000*****我可以使用任何度量作为与中位数的距离(与中位数的绝对偏差、分位数等)。 最佳答案 如果您正在寻找如何计算MedianAbsoluteDevi

python pandas dataframe 从其他列的单元格创建新列

我有一个这样的数据框...a_returnb_returnbc_ratioinstrument_holding0NaNNaN-0.165286a10.9964741.013166-0.164637a20.9977300.993540-0.170058a31.0242941.024318-0.184530a41.0190711.047297-0.148644a50.9922431.008210-0.188752a61.0103311.039020-0.098413a70.9895420.9918990.025051b81.0051971.002527-0.025051b90.990755

python - 报告实验室。具有两列的 float 文本

首先,我是python、reportlab、xhtml2pdf的新手。我已经用reportlab完成了我的第一个pdf文件,但我遇到了以下问题。我需要两列的大文本。首先我创建我的Canvas,创建我的故事,将我的大文本作为段落附加到故事中,创建我的框架,最后将故事添加到框架中。c=Canvas("local.pdf")storyExample=[]textExample=("""ThisisaverylargetextLoremIpsum...""")storyExample.append(Paragraph(textExample,styleText))frameExample=Fr

python - 在每个 pandas 数据框行中查找前 n 个最高值列的名称

我有以下数据框:idp1p2p3p4109142023431310741531523710我需要reshape数据框,使每个ID的前3列具有最高值。结果会是这样的:idtop1top2top31p2p4p32p4p3p23p3p4p24p2p3p4/p15p4p3p2它显示每个user_id的前3名畅销书。我已经使用R中的dplyr包完成了它,但我正在寻找等效的pandas。 最佳答案 你可以使用np.argsort为每行找到n个最大项目的索引:importnumpyasnpimportpandasaspddf=pd.DataFra

python - 第 1 行 'url' 列的数据被截断 - 使用 Python Django 编程时出错

我得到的错误信息是Traceback(mostrecentcalllast):File"./test.py",line416,instartup()File"./test.py",line275,instartupwriter.save(r,data)File"/home/user/project/test/output.py",line91,insaveself.save_doc(r,data,pid)File"/home/user/project/test/output.py",line130,insave_doccursor.execute(dbquery)File"/usr/l

python - 如何更改 DataFrame 中一列的数据类型?

我想更改一个数据框列的数据类型(从datetime64到对象)。首先,我创建数据框:Python2.6.8(unknown,Jan262013,14:35:25)[GCC4.7.2]onlinux2Type"help","copyright","credits"or"license"formoreinformation.>>>importpandasaspd>>>values=pd.Series(iforiinrange(5))>>>dates=pd.date_range('20130101',periods=5)>>>df=pd.DataFrame({'values':values,

python - 如何进行查询以过滤其中一列等于同一表中另一列的行?

假设我有一个看起来像这样的模型:classStockRequest(models.Model):amount_requested=models.PositiveIntegerField(null=True)amount_approved=models.PositiveIntegerField(null=True)有什么方法可以使Django查询显示所有请求,其中在特定对象/行上的amount_requested和amount_approved之间存在某种关系?在SQL中它会很简单:select*fromstockrequestwhereamount_requested=amount_a

python - 将新列计算为 Pandas 中其他列的平均值

这个问题在这里已经有了答案:Row-wiseaverageforasubsetofcolumnswithmissingvalues(3个答案)关闭4年前。我有一个这个数据框,我想计算一个新列作为salary_1、salary_2和salary_3的平均值:df=pd.DataFrame({'salary_1':[230,345,222],'salary_2':[235,375,292],'salary_3':[210,385,260]})salary_1salary_2salary_3023023521013453753852222292260我怎样才能以最有效的方式在Pandas中做