Pandas 计算数据框中列本身内的数据
I have come up with a problem where my data in the column has been recorded as 90-2,91-3,90+4 etc.My motive here is to add and subtract the data directly into the column itself. Datatype of the column is an object.
df = df1["ldm"].str.split('+',expand =True)
if df.shape[1]>1:
df_2 = df[0].str.split('-',expand = True)
df_2 = df_2.fillna(value=0)
df = df.fillna(value=0)
df_2[0] = df_2[0].astype(int)
df[1] = df[1].astype(int)
df_2[1] = df_2[1].astype(int)
df_2['3'] = df[1]
df_2[0]=df_2[0]-df_2[1]
df_2[0] = df_2[0]+df_2['3']
df1['ldm'] = df_2[0]
这是我效率低下的解决方案。我正在寻找一种有效的方法来在数据框中计算它。
I have come up with a problem where my data in the column has been recorded as 90-2,91-3,90+4 etc.My motive here is to add and subtract the data directly into the column itself. Datatype of the column is an object.
df = df1["ldm"].str.split('+',expand =True)
if df.shape[1]>1:
df_2 = df[0].str.split('-',expand = True)
df_2 = df_2.fillna(value=0)
df = df.fillna(value=0)
df_2[0] = df_2[0].astype(int)
df[1] = df[1].astype(int)
df_2[1] = df_2[1].astype(int)
df_2['3'] = df[1]
df_2[0]=df_2[0]-df_2[1]
df_2[0] = df_2[0]+df_2['3']
df1['ldm'] = df_2[0]
This is my inefficient solution..I am looking for an efficient way to compute this in the dataframe.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
pandas.eval
。它支持有限范围的操作,这使得它使用起来比 python 的eval
更安全,也比ast.literal_eval
更方便。从文档中:
输出:
Use
pandas.eval
. It supports a limited range of operations, which makes it much safer to use than python'seval
and more convenient thanast.literal_eval
.From the documentation:
output: