【Python:如何从pandas系列中获取字典中的值】教程文章相关的互联网学习教程文章

python – Pandas中数据帧子集的随机样本【代码】

假设我有一个包含100,000个条目的数据框,并希望将其拆分为1000个条目的100个部分. 我如何采取100个部分中只有一部分的50个随机样本.数据集已经被排序,前1000个结果是下一个部分的第一个部分,依此类推. 非常感谢解决方法:您可以使用sample方法*:In [11]: df = pd.DataFrame([[1, 2], [3, 4], [5, 6], [7, 8]], columns=["A", "B"])In [12]: df.sample(2) Out[12]:A B 0 1 2 2 5 6In [13]: df.sample(2) Out[13]:A B 3 7 8 ...

python – Pandas groupby与dict【代码】

是否可以使用dict对列的元素进行分组? 例如:In [3]: df = pd.DataFrame({'A' : ['one', 'one', 'two', 'three','two', 'two', 'one', 'three'],...: 'B' : np.random.randn(8)}) In [4]: df Out[4]: A B 0 one 0.751612 1 one 0.333008 2 two 0.395667 3 three 1.636125 4 two 0.916435 5 two 1.076679 6 one -0.992324 7 three -0.593476In [5]: d = {'one':'Start', 'two':'Start'...

创建单行python pandas数据帧【代码】

我想创建一个带有单行的python pandas DataFrame,以使用进一步的pandas功能,如转储到* .csv. 我已经看到使用了以下代码,但我最终只得到列结构,但是空数据import pandas as pddf = pd.DataFrame() df['A'] = 1 df['B'] = 1.23 df['C'] = "Hello" df.columns = [['A','B','C']]print dfEmpty DataFrame Columns: [A, B, C] Index: []虽然我知道还有其他方法可以做到这一点(比如来自字典),但我想了解为什么这段代码对我不起作用!?这...

python – 将pandas dataframe列从十六进制字符串转换为int【代码】

我有一个非常大的数据框,我想避免遍历每一行,并希望将整个列从十六进制字符串转换为int.它不会使用astype正确处理字符串,但单个条目没有问题.有没有办法告诉astype数据类型是16?IN: import pandas as pd df = pd.DataFrame(['1C8','0C3'], columns=['Command0']) df['Command0'].astype(int) OUT: ValueError: invalid literal for int() with base10: '1C8'这有效,但希望避免行迭代.for index, row in df.iterrows():print(row['...

python – 如何使用pandas read_csv和gzip压缩选项读取tar.gz文件?【代码】

我有一个非常简单的csv,其中包含以下数据,在tar.gz文件中压缩.我需要使用pandas.read_csv在数据框中读取它.A B 0 1 4 1 2 5 2 3 6import pandas as pd pd.read_csv("sample.tar.gz",compression='gzip')但是,我收到错误:CParserError: Error tokenizing data. C error: Expected 1 fields in line 440, saw 2以下是read_csv命令的集合以及我得到的不同错误:pd.read_csv("sample.tar.gz",compression='gzip', engine='pyt...

导入Pandas会产生错误AttributeError:模块’pandas’在iPython Notebook中没有属性’core’【代码】

我正在通过Anaconda Navigator应用程序(版本1.1.0)运行iPython笔记本.当我想导入大熊猫时,它给了我一个奇怪的错误.我认为Anaconda应用程序包括熊猫包?--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-4-af55e7023913> in <module>() ----> 1 import pandas as pd/Users/bertcarremans/anaconda/lib/p...

python – Pandas DafaFrame中的舍入条目【代码】

使用:newdf3.pivot_table(rows=['Quradate'],aggfunc=np.mean)产量:Alabama_exp Credit_exp Inventory_exp National_exp Price_exp Sales_exp Quradate 2010-01-15 0.568003 0.404481 0.488601 0.483097 0.431211 0.570755 2010-04-15 0.543620 0.385417 0.455078 0.468750 0.408203 0.564453我想把十进制数字四舍五入到两位数乘以100,例如.568003应该是57在...

python – csv中的双引号元素不能用pandas读取【代码】

我有一个输入文件,其中每个值都存储为一个字符串.它位于一个csv文件中,每个条目都在双引号内. 示例文件:"column1","column2", "column3", "column4", "column5", "column6" "AM", "07", "1", "SD", "SD", "CR" "AM", "08", "1,2,3", "PR,SD,SD", "PR,SD,SD", "PR,SD,SD" "AM", "01", "2", "SD", "SD", "SD"只有六列.我需要输入哪些选项来pandas read_csv才能正确读取? 我目前正在尝试:import pandas as pd df = pd.read_csv(fil...

python – Pandas:在ID上拆分数据帧并使用生成的文件名写入csv【代码】

我有一个我想迭代的pandas数据帧.例如,我的数据框的简化版本可以是:chr start end Gene Value MoreData chr1 123 123 HAPPY 41.1 3.4 chr1 125 129 HAPPY 45.9 4.5 chr1 140 145 HAPPY 39.3 4.1 chr1 342 355 SAD 34.2 9.0 chr1 360 361 SAD 44.3 8.1 chr1 390 399 SAD 29.0 7.2 chr1 400 411 SAD 35.6 6....

python – 从pandas数据框中删除具有空值的行【代码】

我正在尝试从我的数据框中删除一行,其中一列的值为null.我能找到的大部分帮助都与去除NaN值有关,这些值迄今为止对我没用. 在这里,我创建了数据框:# successfully crated data framedf1 = ut.get_data(symbols, dates) # column heads are 'SPY', 'BBD'# can't get rid of row containing null val in column BBD # tried each of these with the others commented out but always had an # error or sometimes I was able to get...

python – Pandas DataFrame日期索引的偏移日期【代码】

给定Pandas数据帧如下:dates = pd.date_range('20130101',periods=6) df = pd.DataFrame(np.random.randn(6),index=dates,columns=list('A'))A 2013-01-01 0.847528 2013-01-02 0.204139 2013-01-03 0.888526 2013-01-04 0.769775 2013-01-05 0.175165 2013-01-06 -1.564826我想在索引中添加15天.这不起作用>#from pandas.tseries.offsets import * df.index+relativedelta(days=15) #df.index + DateOffset(days=5)Ty...

python – Pandas中的多个直方图【代码】

我想创建以下直方图(见下图)取自“Think Stats”一书.但是,我无法将它们放在同一个地块上.每个DataFrame都有自己的子图. 我有以下代码:import nsfg import matplotlib.pyplot as plt df = nsfg.ReadFemPreg() preg = nsfg.ReadFemPreg() live = preg[preg.outcome == 1]first = live[live.birthord == 1] others = live[live.birthord != 1]#fig = plt.figure() #ax1 = fig.add_subplot(111)first.hist(column = 'prglngth', bins...

python – Pandas数据帧 – 运行与重置的和【代码】

我想计算给定列中的运行总和(当然不使用循环).需要注意的是,我有另一列指定何时将运行总和重置为该行中存在的值.最佳解释如下:reset val desired_col 0 0 1 1 1 0 5 6 2 0 4 10 3 1 2 2 4 1 -1 -1 5 0 6 5 6 0 4 9 7 1 2 2desired_col是我想要计算的值.解决方法:你可以使用2次cumsum():# reset val desired_col #0 0 1 ...

python – pandas dataframe删除常量列【代码】

我有一个数据框,可能有也可能没有相同值的列.例如row A B1 9 02 7 03 5 04 2 0我想回来row A 1 9 2 7 3 5 4 2是否有一种简单的方法来识别是否存在这些列中的任何一列然后将其删除?解决方法:我相信这个选项会比这里的其他答案更快,因为如果找到非唯一值,它只会遍历数据帧一次以进行比较和短路.>>> df0 1 2 0 1 9 0 1 2 7 0 2 3 7 0>>> df.loc[...

python – 在pandas.read_csv()中使用自定义对象【代码】

我有兴趣将自定义对象流式传输到pandas数据帧中.根据the documentation,可以使用具有read()方法的任何对象.但是,即使在实现此功能后,我仍然会收到此错误:ValueError: Invalid file path or buffer object type: <class ‘__main__.DataFile’>这是对象的简单版本,以及我如何调用它:class DataFile(object):def __init__(self, files):self.files = filesdef read(self):for file_name in self.files:with open(file_name, 'r') a...

字典 - 相关标签