【python – 合并pandas数据帧,其中一个值介于两个其他值之间】教程文章相关的互联网学习教程文章

python – 按标签选择的Pandas有时会返回Series,有时会返回DataFrame【代码】

在Pandas中,当我选择一个只在索引中有一个条目的标签时,我会返回一个系列,但是当我选择一个包含多个条目的条目时,我会返回一个数据框. 这是为什么?有没有办法确保我总能找回数据框?In [1]: import pandas as pdIn [2]: df = pd.DataFrame(data=range(5), index=[1, 2, 3, 3, 3])In [3]: type(df.loc[3]) Out[3]: pandas.core.frame.DataFrameIn [4]: type(df.loc[1]) Out[4]: pandas.core.series.Series解决方法:假设行为不一致,...

python – 将pandas Series转换为DataFrame【代码】

我有一个熊猫系列sf:email email1@email.com [1.0, 0.0, 0.0] email2@email.com [2.0, 0.0, 0.0] email3@email.com [1.0, 0.0, 0.0] email4@email.com [4.0, 0.0, 0.0] email5@email.com [1.0, 0.0, 3.0] email6@email.com [1.0, 5.0, 0.0]我想将其转换为以下DataFrame:index | email | list _____________________________________________ 0 | email1@email.com | [1.0, 0.0, 0.0] 1 |...

python – 与NaNs相等比较的Pandas DataFrames【代码】

在单元测试一些函数的上下文中,我试图使用python pandas建立2个DataFrame的相等性:ipdb> expect1 2 2012-01-01 00:00:00+00:00 NaN 3 2013-05-14 12:00:00+00:00 3 NaNipdb> df identifier 1 2 timestamp 2012-01-01 00:00:00+00:00 NaN 3 2013-05-14 12:00:00+00:00 3 NaNipdb> df[1][0] nanipdb> df[1][0], expect[1][0] (nan, nan)ipdb> df[1][0] == expect[1][0] Falseipdb> df[1][1] == expect...

python – Pandas – 替换列值【代码】

我知道这个问题有很多主题,但没有一种方法适合我,所以我发布了我的具体情况 我有一个如下所示的数据框:data = pd.DataFrame([[1,0],[0,1],[1,0],[0,1]], columns=["sex", "split"]) data['sex'].replace(0, 'Female') data['sex'].replace(1, 'Male') data我想要做的是将性别列中的所有0替换为“女性”,将所有1替换为“男性”,但是当我使用上面的代码时,数据框中的值似乎没有改变 我是否错误地使用了replace()?或者有更好的方法来...

python – 从谷歌云存储中读取csv到pandas数据帧【代码】

我正在尝试将Google Cloud Storage存储桶中的csv文件读取到熊猫数据框中.import pandas as pd import matplotlib.pyplot as plt import seaborn as sns %matplotlib inline from io import BytesIOfrom google.cloud import storagestorage_client = storage.Client() bucket = storage_client.get_bucket('createbucket123') blob = bucket.blob('my.csv') path = "gs://createbucket123/my.csv" df = pd.read_csv(path)它显示以下...

python – 为什么pandas滚动使用单维ndarray【代码】

我有动力使用熊猫滚动功能来执行滚动多因素回归(这个问题不是关于滚动多因素回归).我希望我能够在df.rolling(2)之后使用apply并获取生成的pd.DataFrame,用.values提取ndarray并执行必要的矩阵乘法.它没有那么成功. 这是我发现的:import pandas as pd import numpy as npnp.random.seed([3,1415]) df = pd.DataFrame(np.random.rand(5, 2).round(2), columns=['A', 'B']) X = np.random.rand(2, 1).round(2)对象是什么样的:print ...

python – pandas:多列的to_numeric【代码】

我正在使用以下df:c.sort_values('2005', ascending=False).head(3)GeoName ComponentName IndustryId IndustryClassification Description 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 37926 Alabama Real GDP by state 9 213 Support activities for mining 99 98 117 117 115 87 96 95 103 102 (NA) 37951 ...

python – 如何在Pandas系列中找到输入数字中最接近的值?【代码】

我见过: > how do I find the closest value to a given number in an array?> How do I find the closest array element to an arbitrary (non-member) number?. 这些与香草Python有关,而不是熊猫. 如果我有这个系列:ix num 0 1 1 6 2 4 3 5 4 2我输入3,我怎样才能(有效地)找到? >如果在系列中找到,则索引为3>如果在系列中找不到,则低于和高于3的值的索引. IE浏览器.使用上面的系列{1,6,4,5,2}和输入3,我应...

python – pandas系列和单列DataFrame有什么区别?

为什么pandas会区分Series和单列DataFrame?换句话说:Series类存在的原因是什么? 我主要使用日期时间索引的时间序列,这可能有助于设置上下文.解决方法:引用Pandas docspandas.DataFrame(data=None, index=None, columns=None, dtype=None, copy=False) Two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes(rows and columns). Arithmetic operations align on both row and colu...

python – 如何获取pandas DataFrame的行数?【代码】

我正在尝试用Pandas获取dataframe df的行数,这是我的代码. 方法1:total_rows = df.count print total_rows +1方法2:total_rows = df['First_columnn_label'].count print total_rows +1两个代码片段都给我这个错误:TypeError: unsupported operand type(s) for +: ‘instancemethod’ and ‘int’我究竟做错了什么?解决方法:您可以使用.shape属性或只使用len(DataFrame.index).但是,有显着的性能差异(len(DataFrame.index)最快...

python 数据分析之pandas【图】

pandas 是数据分析时必须用到的一个库,功能非常强大 其有两种数据结构:一维Series 二维表DataFrame(一般读取后的数据都是df) 导入:import pandas as pd 数据读取:pd.read_csv(d:/a.csv,dtype=objec,encoding=utf-8)pd.read_csv(d:/a.txt,dtype=objec,encoding=utf-8)  pd.read_excel(d:/a.xls,dtype=objec,encoding=utf-8) dtype:指定数据读取后的类型 encoding:指定编码 ju...

python – pandas 0.21.0 matplotlib的时间戳兼容性问题【代码】

我刚刚将pandas从0.17.1更新到0.21.0以利用一些新的功能,并遇到matplotlib的兼容性问题(我也更新到最新的2.1.0).特别是,Timestamp对象似乎发生了显着变化. 我碰巧有另一台机器仍在运行旧版本的pandas(0.17.1)/ matplotlib(1.5.1),我曾用它来比较差异: 两个版本都显示我的DataFrame索引为dtype =’datetime64 [ns]DatetimeIndex(['2017-03-13', '2017-03-14', ... '2017-11-17'], type='datetime64[ns]', name='dates', length=170...

python – “unstack”包含多列行列表的pandas列【代码】

参见英文答案 > How to unnest (explode) a column in a pandas DataFrame? 11个假设我有以下Pandas Dataframe:df = pd.DataFrame({"a" : [1,2,3], "b" : [[1,2],[2,3,4],[5]]})a b 0 1 [1, 2] 1 2 [2, 3, 4] 2 3 [5]我如何“取消堆叠”“b”列中的列表以将其转换为数据帧:a b 0 1 1 1 1 2 2 2 2 3 2 3 4 2 4 5 3 5解决方法:更新:通用矢量化方法 – 也...

python – 即使在使用.loc之后,Pandas仍然会获得SettingWithCopyWarning【代码】

起初,我尝试编写一些看起来像这样的代码:import numpy as np import pandas as pd np.random.seed(2016) train = pd.DataFrame(np.random.choice([np.nan, 1, 2], size=(10, 3)), columns=['Age', 'SibSp', 'Parch'])complete = train.dropna() complete['AgeGt15'] = complete['Age'] > 15获得SettingWithCopyWarning后,我尝试使用.loc:complete.loc[:, 'AgeGt15'] = complete['Age'] > 15 complete.loc[:, 'WithFamily'] =...

python – 基于值的Pandas查找【代码】

我有以下DataFrame:Date best a b c d 1990 a 5 4 7 2 1991 c 10 1 2 0 1992 d 2 1 4 12 1993 a 5 8 11 6我想制作如下数据帧:Date best value 1990 a 5 1991 c 2 1992 d 12 1993 a 5所以我希望通过使用列名来找到基于另一个行值的值.例如,第二个df中1990的值应该从第一个df中查找“a”,第二...