Sorting a Dataframe column consisting of Sympy symbols

huangapple go评论76阅读模式
英文:

Sorting a Dataframe column consisting of Sympy symbols

问题

# 导入所需的库
import sympy as sym
import pandas as pd

# 定义Sympy符号和对应的值
d1, c, bc = sym.symbols("δ, c, b_c")
values = [(d1, 1), (c, 2), (bc, 3)]

# 创建DataFrame
df = pd.DataFrame(values, columns=['Symbol', 'Value'])

# 使用.name方法对符号进行排序
sorted_df = df.sort_values(by='Symbol', key=lambda x: x['Symbol'].name)

# 打印排序结果
display(sorted_df)

请注意,以上是根据您提供的信息翻译的代码。如果您有任何问题或需要进一步的帮助,请随时告诉我。

英文:

I have a dataframe, where one column consists of Sympy symbols, and another column that consists of values.

import sympy as sym
import pandas as pd
d1,c,bc = sym.symbols("\delta, c, b_c")

values = [(d1,1),(c,2),(bc,3)]
df = pd.DataFrame(values, columns = ['Symbol', 'Value'])
df['Symbol'].sort_values()

When I run the above, I get the error (which was expected, because Sympy symbols aren't sortable themselves, but the actual string contained is sortable.

TypeError: cannot determine truth value of Relational

Sympy symbols are sortable if you can apply the .name method to them. I've done this with numpy arrays and lists of dictionaries:

import numpy as np
values = np.array(values, dtype = [('Symbol','O'),('Value','O')])
values = sorted(values, key = lambda x: x['Symbol'].name)
display(values)

Output:

>>[(\delta, 1), (b_c, 3), (c, 2)]

I'm wondering if it's possible with dataframes because I'd rather not convert to a different format to apply a sort.

答案1

得分: 2

我不确定这是否更有效,但也许你可以为数据框创建一个新列,例如'Symbol_names',其中只包含名称,并按该列进行排序。你始终可以在之后删除该列。

df['Symbol_names'] = df['Symbol'].apply(lambda x: x.name)

df = df.sort_values('Symbol_names')\
       .drop("Symbol_names", axis=1)\
       .reset_index(drop=True) # 可选
英文:

I am not sure this is more efficient but perhaps you could create a new column e.g 'Symbol_names' for the data frame with names only and sort by that. You can always drop the column after

df['Symbol_names'] = df['Symbol'].apply(lambda x: x.name) 

df = df.sort_values('Symbol_names')\
       .drop("Symbol_names", axis=1)\
       .reset_index(drop=True) # optional

huangapple
  • 本文由 发表于 2020年1月4日 00:29:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582031.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定