Sorting a Dataframe column consisting of Sympy symbols

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

Sorting a Dataframe column consisting of Sympy symbols

问题

  1. # 导入所需的库
  2. import sympy as sym
  3. import pandas as pd
  4. # 定义Sympy符号和对应的值
  5. d1, c, bc = sym.symbols("δ, c, b_c")
  6. values = [(d1, 1), (c, 2), (bc, 3)]
  7. # 创建DataFrame
  8. df = pd.DataFrame(values, columns=['Symbol', 'Value'])
  9. # 使用.name方法对符号进行排序
  10. sorted_df = df.sort_values(by='Symbol', key=lambda x: x['Symbol'].name)
  11. # 打印排序结果
  12. display(sorted_df)

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

英文:

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

  1. import sympy as sym
  2. import pandas as pd
  3. d1,c,bc = sym.symbols("\delta, c, b_c")
  4. values = [(d1,1),(c,2),(bc,3)]
  5. df = pd.DataFrame(values, columns = ['Symbol', 'Value'])
  6. 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.

  1. 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:

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

Output:

  1. >>[(\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',其中只包含名称,并按该列进行排序。你始终可以在之后删除该列。

  1. df['Symbol_names'] = df['Symbol'].apply(lambda x: x.name)
  2. df = df.sort_values('Symbol_names')\
  3. .drop("Symbol_names", axis=1)\
  4. .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

  1. df['Symbol_names'] = df['Symbol'].apply(lambda x: x.name)
  2. df = df.sort_values('Symbol_names')\
  3. .drop("Symbol_names", axis=1)\
  4. .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:

确定