【python – 为什么pandas.DataFrame.update会更改更新数据帧的dtypes?】教程文章相关的互联网学习教程文章

python – PySpark DataFrame上的Sum运算在type为fine时给出TypeError【代码】

我在PySpark中有这样的DataFrame(这是take(3)的结果,数据帧非常大):sc = SparkContext() df = [Row(owner=u'u1', a_d=0.1), Row(owner=u'u2', a_d=0.0), Row(owner=u'u1', a_d=0.3)]同一所有者将拥有更多行.我需要做的是在分组之后将每个所有者的字段a_d的值相加为b = df.groupBy('owner').agg(sum('a_d').alias('a_d_sum'))但这会引发错误TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’但是,架构包含双精度...

python – 以字符串元组作为索引的Pandas DataFrame【代码】

我在这里感觉到一些奇怪的熊猫行为.我有一个看起来像的数据框df = pd.DataFrame(columns=['Col 1', 'Col 2', 'Col 3'],index=[('1', 'a'), ('2', 'a'), ('1', 'b'), ('2', 'b')])In [14]: df Out[14]:Col 1 Col 2 Col 3 (1, a) NaN NaN NaN (2, a) NaN NaN NaN (1, b) NaN NaN NaN (2, b) NaN NaN NaN我可以设置任意元素的值In [15]: df['Col 2'].loc[('1', 'b')] = 6In [16]: df Out[16]:Col 1 Col 2 Col...

python – 如何在pandas dataframe列中选择一系列值?【代码】

import pandas as pd import numpy as np data = 'filename.csv' df = pd.DataFrame(data) df one two three four five a 0.469112 -0.282863 -1.509059 bar True b 0.932424 1.224234 7.823421 bar False c -1.135632 1.212112 -0.173215 bar False d 0.232424 2.342112 0.982342 unbar True e 0.119209 -1.044236 -0.861849 bar True f -2.104569 -0.494929 1.071804 bar False我想为某一列...

python – Pandas DataFrame合并求和列【代码】

我正在尝试合并两个DataFrames求和列值. DF1id name weight 1 A 02 B 103 C 10DF2id name weight 2 B 153 C 10我需要在合并期间对公共列中的类似值求和权重值.merge = pd.merge(df1,df2, how='inner')所以输出将如下所示.id name weight 2 B 253 ...

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 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 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 – 基于先前获取的子集从DataFrame中删除行【代码】

我正在运行安装了Pandas 0.11.0库的Python 2.7. 我一直在寻找一个没有找到这个问题的答案,所以我希望有人比我有解决方案更有经验. 让我们说我的数据,在df1中,如下所示: DF1 =zip x y access123 1 1 4123 1 1 6133 1 2 3145 2 2 3167 3 1 1167 3 1 2例如,使用df2 = df1 [df1 [‘zip’] == 123]然后df2 = df2.join(df1 [df1 [‘zip’] == 133])我得到以下数据子集: DF2 =zip x y access123 1...

python – 有效地将列中的值替换为另一列Pandas DataFrame【代码】

我有一个像下面这样的Pandas DataFrame:col1 col2 col3 1 0.2 0.3 0.3 2 0.2 0.3 0.3 3 0 0.4 0.4 4 0 0 0.3 5 0 0 0 6 0.1 0.4 0.4我想要将col1值替换为第二列(col2)中的值,仅当col1值等于0时,并且(对于剩余的零值)之后,再次使用第三列(col3)进行替换.期望的结果是下一个:col1 col2 col3 1 0.2 0.3 0.3 2 0.2 0.3 0.3 3 0.4 0.4 0.4 4 0.3 0 0.3 5 0 0 0 6 ...

python – 将numpy.array存储在Pandas.DataFrame的单元格中【代码】

我有一个数据框,我想在其中存储’raw’numpy.array:df['COL_ARRAY'] = df.apply(lambda r: np.array(do_something_with_r), axis=1)但似乎熊猫试图’解包’numpy.array. 有解决方法吗?除了使用包装器(参见下面的编辑)? 我试过reduce = False没有成功. 编辑 这是有效的,但是我必须使用’dummy’Data类来包围数组,这是不令人满意的并且不是很优雅.class Data:def __init__(self, v):self.v = vmeas = pd.read_excel(DATA_FILE) me...

python – 融合Pandas Dataframe的上三角矩阵【代码】

给定以下形式的方形pandas DataFrame:a b c a 1 .5 .3 b .5 1 .4 c .3 .4 1我怎样才能融化上三角形才能得到Row Column Valuea a 1a b .5 a c .3b b 1b c .4c c 1 #Note the combination a,b is only listed once. There is no b,a listing 我对一个惯用的熊猫解决方案更感兴趣,一个自定义索引器很容易手工编写…提前感谢您的考虑...

python – Pandas将Dataframe拆分为两个Dataframe【代码】

我有pandas DataFrame,我是用concat编写的.一行由96个值组成,我想从值72中拆分DataFrame. 这样一行的前72个值存储在Dataframe1中,而下一个24个值存储在Dataframe2中. 我创建我的DF如下:temps = DataFrame(myData) datasX = concat( [temps.shift(72), temps.shift(71), temps.shift(70), temps.shift(69), temps.shift(68), temps.shift(67),temps.shift(66), temps.shift(65), temps.shift(64), temps.shift(63), temps.shift(62...

python – 向具有特定索引名称的Pandas DataFrame添加新行【代码】

我正在尝试使用特定索引名称“e”向DataFrame添加新行.number variable values a NaN bank true b 3.0 shop false c 0.5 market true d NaN government true 我尝试了以下但是它创建了一个新列而不是一个新行.new_row = [1.0, 'hotel', 'true'] df = df.append(new_row)仍然不明白如何插入具有特定索引的行.将不胜感激任何建议.解决方法:您可以...

python – 将数据从Dataframe传递到现有ML VectorIndexerModel时出错【代码】

我有一个Dataframe,我想用它来预测现有的模型.使用模型的transform方法时出错. 这就是我处理trainingdata的方法.forecast.printSchema()我的Dataframe的架构:root|-- PM10: double (nullable = false)|-- rain_3h: double (nullable = false)|-- is_rain: double (nullable = false)|-- wind_deg: double (nullable = false)|-- wind_speed: double (nullable = false)|-- humidity: double (nullable = false)|-- is_newYear: do...

python – 子类化Pandas DataFrame,更新?【代码】

继承还是不继承? Pandas子类化问题的最新内容是什么? (大多数其他线程都是3-4岁). 我希望做点像……import pandas as pdclass SomeData(pd.DataFrame):# MethodspassClsInstance = SomeData()# Create a new column on ClsInstance?解决方法:这就是我做到的.我遵循了以下建议: > subclassing-pandas-data-structures> Fix Finalize Issue 下面的示例仅显示了构建pandas.DataFrame的新子类的用法.如果您遵循我的第一个链接中的建...