Spring控制台中的表达式语言(SpEL)(Spring expression language (SpEL) in a console)
Spring表达式语言的外壳是否有一个带有外壳的摆动控制台?
Is there something somewhere with a swing console with a shell for Spring Expression Language?
原文:https://stackoverflow.com/questions/6681667
最满意答案
使用原始df1索引创建系列:
df1['e'] = Series(np.random.randn(sLength), index=df1.index)
编辑2015
有些报告使用此代码获取SettingWithCopyWarning
。
然而,现在的大熊猫版本0.16.1,代码仍然运行完美。>>> sLength = len(df1['a']) >>> df1 a b c d 6 -0.269221 -0.026476 0.997517 1.294385 8 0.917438 0.847941 0.034235 -0.448948 >>> df1['e'] = p.Series(np.random.randn(sLength), index=df1.index) >>> df1 a b c d e 6 -0.269221 -0.026476 0.997517 1.294385 1.757167 8 0.917438 0.847941 0.034235 -0.448948 2.228131 >>> p.version.short_version '0.16.1'
SettingWithCopyWarning
旨在通知Dataframe副本中的可能无效的分配。 它不一定说你做错了(它可以触发误报),但从0.13.0它让你知道有更多的适当的方法为同一目的。 然后,如果您收到警告,只需按照其建议: 尝试使用.loc [row_index,col_indexer] =值>>> df1.loc[:,'f'] = p.Series(np.random.randn(sLength), index=df1.index) >>> df1 a b c d e f 6 -0.269221 -0.026476 0.997517 1.294385 1.757167 -0.050927 8 0.917438 0.847941 0.034235 -0.448948 2.228131 0.006109 >>>
事实上,这是目前较为有效的方法, 如pandas docs所述
编辑2017
如在注释和@Alexander中所指出的,目前将Series的值添加为DataFrame的新列的最佳方法可能是使用
assign
:df1 = df1.assign(e=p.Series(np.random.randn(sLength)).values)
Use the original df1 indexes to create the series:
df1['e'] = Series(np.random.randn(sLength), index=df1.index)
Edit 2015
Some reported getting theSettingWithCopyWarning
with this code.
However, the code still runs perfectly with the current pandas version 0.16.1.>>> sLength = len(df1['a']) >>> df1 a b c d 6 -0.269221 -0.026476 0.997517 1.294385 8 0.917438 0.847941 0.034235 -0.448948 >>> df1['e'] = p.Series(np.random.randn(sLength), index=df1.index) >>> df1 a b c d e 6 -0.269221 -0.026476 0.997517 1.294385 1.757167 8 0.917438 0.847941 0.034235 -0.448948 2.228131 >>> p.version.short_version '0.16.1'
The
SettingWithCopyWarning
aims to inform of a possibly invalid assignment on a copy of the Dataframe. It doesn't necessarily say you did it wrong (it can trigger false positives) but from 0.13.0 it let you know there are more adequate methods for the same purpose. Then, if you get the warning, just follow its advise: Try using .loc[row_index,col_indexer] = value instead>>> df1.loc[:,'f'] = p.Series(np.random.randn(sLength), index=df1.index) >>> df1 a b c d e f 6 -0.269221 -0.026476 0.997517 1.294385 1.757167 -0.050927 8 0.917438 0.847941 0.034235 -0.448948 2.228131 0.006109 >>>
In fact, this is currently the more efficient method as described in pandas docs
Edit 2017
As indicated in the comments and by @Alexander, currently the best method to add the values of a Series as a new column of a DataFrame could be using
assign
:df1 = df1.assign(e=p.Series(np.random.randn(sLength)).values)
相关问答
更多-
基于python中的pandas索引在新列中添加值(adding values in new column based on indexes with pandas in python)[2023-07-05]
按Series重命名index : df2['new'] = df2.rename(index=df.set_index('key')['bar']).index print (df2) other_data new key a 10 1 a 20 1 b 30 2 或者map : df2['new'] = df2.index.to_series().map(df.set_index( ... -
你只是做一个相反的比较。 if Col2 <= 1 。 这将返回一个布尔系列,其中大于1的False值和另一个的True值。 如果将其转换为int64 dtype,则True 1, False为0 , df['Col3'] = (df['Col2'] <= 1).astype(int) 如果您想要更一般的解决方案,您可以根据Col2的值分配任何号码到Col3 ,您应该执行以下操作: df['Col3'] = df['Col2'].map(lambda x: 42 if x > 1 else 55) 要么 ...
-
df2.loc['new_row'] = list 假设'new_row'不在cont_index 。 df2.loc['new_row'] = list Assuming 'new_row' is not in cont_index.
-
使用原始df1索引创建系列: df1['e'] = Series(np.random.randn(sLength), index=df1.index) 编辑2015 有些报告使用此代码获取SettingWithCopyWarning 。 然而,现在的大熊猫版本0.16.1,代码仍然运行完美。 >>> sLength = len(df1['a']) >>> df1 a b c d 6 -0.269221 -0.026476 0.99751 ...
-
实际上并不是熊猫的问题。 上面的场景是通过右键单击将代码粘贴到iPython中创建的。 使用%paste magic功能(如文档所推荐)不会产生此问题。 Not in fact an issue with Pandas. The above scenario was created by pasting the code into iPython by right-clicking. Using the %paste magic function (as the documentation recommen ...
-
你可以使用eq和axis=0比较df和maxval ,然后使用apply和lambda生成一个布尔掩码来掩盖列并join它们: In [183]: df['maxcol'] = df.ix[:,:'c'].eq(df['maxval'], axis=0).apply(lambda x: ','.join(df.columns[:3][x==x.max()]),axis=1) df Out[183]: a b c maxval maxcol 0 1 0 0 1 a 1 ...
-
假设你有: df1=pd.DataFrame({'col_1': {0: 3, 1: 4, 2: 5}, 'col_2': {0: 7, 1: 8, 2: 5}}) df2=pd.DataFrame({'col_1': {0: 4, 1: 5}, 'col_3': {0: 78, 1: 89}}) 您可以使用合并: pd.merge(df1,df2,on='col_1',how='left').fillna(0) Out[22]: col_1 col_2 col_3 0 3 ...
-
随着assign df.assign(C=[{'M': [1, 2, 3]}]) A B C 0 1 f {'M': [1, 2, 3]} With assign df.assign(C=[{'M': [1, 2, 3]}]) A B C 0 1 f {'M': [1, 2, 3]}
-
根据现有列中的bool值向pandas数据帧添加列(Adding a column to pandas dataframe based on bool values in existing column)[2022-03-23]
你可以使用numpy.where : import numpy as np data_2015['color_code'] = np.where(data_2015['isfixed'], 'Blue', 'Green') 一个演示: df = pd.DataFrame({'isfixed': [True, False, True]}) df Out: isfixed 0 True 1 False 2 True df['color_code'] = np.where(df['i ... -
我不清楚你要做什么,但也许你想要这样的东西? df = DataFrame() def myfunc(number): row_index = 0 for x in range(0,10): if 'some condition': df.loc[row_index, 'results%d' % number] = x row_index += 1 I'm not exactly clear on what you're t ...