[英]How to Sort the Value of List by a Item in Python?
For example, I have dataframe program like:例如,我有如下数据框程序:
lst3 = [
['it store', ['asus', 'acer', 'hp', 'dell'], [50000, 30000, 20000, 10000]],
['mz store', ['acer', 'dell'], [60000, 75000]],
['bm shop', ['hp', 'acer', 'asus'], [45000, 15000, 30000]]
]
df3 = pd.DataFrame(lst3, columns =['store_name', 'item', 'price'], dtype = float)
print(df3)
And the result is:结果是:
store_name item price
0 it store [asus, acer, hp, dell] [50000, 30000, 20000, 10000]
1 mz store [acer, dell] [60000, 75000]
2 bm shop [hp, acer, asus] [45000, 15000, 30000]
The type of column 'item' and 'price' are list. 'item' 和 'price' 列的类型是列表。
So, for example I wanna sort the dataframe by the lowest price of item 'acer'.因此,例如,我想按项目“acer”的最低价格对数据框进行排序。 The expected result is:
预期结果是:
store_name item price
2 bm shop [hp, acer, asus] [45000, 15000, 30000]
0 it store [asus, acer, hp, dell] [50000, 30000, 20000, 10000]
1 mz store [acer, dell] [60000, 75000]
[edit: additional] And, if sort the dataframe by the lowest price of item 'hp', the expected result is: [edit: additional] 而且,如果按项目“hp”的最低价格对数据框进行排序,则预期结果为:
store_name item price
0 it store [asus, acer, hp, dell] [50000, 30000, 20000, 10000]
2 bm shop [hp, acer, asus] [45000, 15000, 30000]
Could you help me, how about the program script to make the result like above in Python?你能帮我吗,用 Python 生成上述结果的程序脚本怎么样?
One of the solutions is to convert the DataFrame
to records using to_records()
method.一种解决方案是使用
to_records()
方法将DataFrame
转换为记录。
Sort it using python's builtin sorted()
function.使用 python 的内置
sorted()
function 对其进行排序。
Then convert back it to DataFrame
using from_records()
.然后使用
from_records()
将其转换回DataFrame
。
For your current DataFrame
to sort price by minimum in the list, you can do following.对于您当前的
DataFrame
按列表中的最低价格排序,您可以执行以下操作。
sorted_records = sorted(df3.to_records(), key=lambda x: min(x[3]))
df3 = pd.DataFrame.from_records(sorted_records)
Keep in track of the index of the column you are trying to sort from when converted to records.在转换为记录时跟踪您尝试排序的列的索引。
It seems that the DataFrame does not contain an easy way to sort by specific-user-defined keys.似乎 DataFrame 不包含按特定用户定义的键进行排序的简单方法。 so you can just create a translation to list and sort it as you wish like so:
所以您可以创建一个翻译来列出并按照您的意愿对其进行排序:
def sort_by_product(df3, product):
def get_product_price(current_store):
current_product = product
return current_store[2][current_store[1].index(current_product)]
sorted_list = sorted(df3.values.tolist(), key=get_product_price)
return pd.DataFrame(sorted_list , columns =['store_name', 'item', 'price'], dtype = float)
usage example:用法示例:
sort_by_product(df3, "acer")
Which outputs:哪个输出:
store_name item price
0 bm shop [hp, acer, asus] [45000, 15000, 30000]
1 it store [asus, acer, hp, dell] [50000, 30000, 20000, 10000]
2 mz store [acer, dell] [60000, 75000]
Hope that helped希望有帮助
This will work only if all the list in column item contains the string acer仅当列 item 中的所有列表都包含字符串acer时,这才有效
import pandas as pd
lst3 = [
['it store', ['asus', 'acer', 'hp', 'dell'], [50000, 30000, 20000, 10000]],
['mz store', ['acer', 'dell'], [60000, 75000]],
['bm shop', ['hp', 'acer', 'asus'], [45000, 15000, 30000]]
]
df3 = pd.DataFrame(lst3, columns =['store_name', 'item', 'price'])
df3['new'] = df3['item'].apply(lambda x: x.index('acer'))
def f(x):
return(x[2][x[3]])
df3['new']=df3.apply(f,axis=1)
df3.sort_values(by=['new'], inplace=True)
df3.drop(['new'], axis=1, inplace=True)
df3.reset_index(drop=True, inplace=True)
df3
The output is as follows: output如下:
store_name item price
0 bm shop [hp, acer, asus] [45000, 15000, 30000]
1 it store [asus, acer, hp, dell] [50000, 30000, 20000, 10000]
2 mz store [acer, dell] [60000, 75000]
I hope this does the work!我希望这能奏效!
You could put whatever computer brand you want to replace 'acer'你可以把任何你想替换'acer'的电脑品牌
from more_itertools import roundrobin as rb
lst3 = [
['it store', ['asus', 'acer', 'hp', 'dell'], [50000, 30000, 20000, 10000]],
['mz store', ['acer', 'dell'], [60000, 75000]],
['bm shop', ['hp', 'acer', 'asus'], [45000, 15000, 30000]]
]
d2 = {}
for k,v in {e[0] : list(rb(e[1], e[2])) for e in lst3}.items():
try:
d2[k]=v[v.index('acer')+1]
except:
continue
ord_lst3 = []
for shop in sorted(d2):
ord_lst3 += list(filter(lambda e: e[0] == shop, lst3))
print(ord_lst3)
# [['bm shop', ['hp', 'acer', 'asus'], [45000, 15000, 30000]],
# ['it store', ['asus', 'acer', 'hp', 'dell'], [50000, 30000, 20000, 10000]],
# ['mz store', ['acer', 'dell'], [60000, 75000]]]
Summary:概括:
item
and price
are related ( item
holds acer
, the index of acer
in item
is directly related to its price
in the price
column). item
和price
相关( item
持有acer
, item
中acer
的索引与其在price
列中的price
直接相关)。 so we need to find a way to pair them.所以我们需要找到一种方法来配对它们。
get the index of acer
in item
column, get its corresponding price
in the price
column, sort from smallest to biggest, get the indices, and use that index to reindex the dataframe:在
item
列中获取acer
的索引,在price
列中获取其对应的price
,从小到大排序,获取索引,并使用该索引重新索引 dataframe:
from operator import itemgetter
#use enumerate to get the numbers attached
#we could also zip the index instead
sorter = sorted([(num,price[item.index('acer')])
for num, (item,price)
in enumerate(zip(df3.item,df3.price))]
,key=itemgetter(1))
#extract only the first item from each tuple in the sorter list
new_index = [first for first,last in sorter]
#reindex dataframe to get our sorted form
df3.reindex(new_index)
store_name item price
2 bm shop [hp, acer, asus] [45000, 15000, 30000]
0 it store [asus, acer, hp, dell] [50000, 30000, 20000, 10000]
1 mz store [acer, dell] [60000, 75000]
IIUC, Series.str.index
and DataFrame.lookup
IIUC、
Series.str.index
和DataFrame.lookup
indexes = df3['item'].str.index('acer')
df = pd.DataFrame(df3['price'].tolist())
(df3.assign(acer_value = df.lookup(df.index , indexes))
.sort_values('acer_value')
.drop(columns='acer_value'))
store_name item price
2 bm shop [hp, acer, asus] [45000, 15000, 30000]
0 it store [asus, acer, hp, dell] [50000, 30000, 20000, 10000]
1 mz store [acer, dell] [60000, 75000]
Or:或者:
order = (df3.assign(indexes = df3['item'].str.index('acer'))
.apply(lambda x: x['price'][x['indexes']], axis=1)
.sort_values().index)
df3.loc[order]
It seems that the DataFrame does not contain an easy way to sort by specific-user-defined keys.似乎 DataFrame 不包含按特定用户定义的键排序的简单方法。 so you can just create a translation to list and sort it as you wish like so:
所以你可以创建一个翻译来列出并按照你的意愿排序:
def sort_by_product(df3, product): def sort_by_product(df3,产品):
def get_product_price(current_store):
current_product = product
return current_store[2][current_store[1].index(current_product)]
sorted_list = sorted(df3.values.tolist(), key=get_product_price)
return pd.DataFrame(sorted_list , columns =['store_name', 'item', 'price'], dtype = float)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.