简体   繁体   English

如何使用 Pandas 对每一行数据进行排序

[英]How can I sort each row data using Pandas

Date        Product  O1_Name  O1_Price  O1_Qty  O2_Name  O2_Price  O2_Qty
05-12-2021  MK       Widget1   0.99      23     Widget2    1.50      80
05-12-2021  MK       Widget2   1.50      85     Widget1    0.99      25
05-11-2021  MK       Widget1   0.99      28     Widget2    1.50      85
05-10-2021  MK       Widget1   0.99      31     Widget2    1.50      95
05-13-2021  PS       WidgetA   0.52      49     WidgetB    0.86      43
05-12-2021  PS       WidgetA   0.52      53     WidgetB    0.86      43
05-10-2021  PS       WidgetB   0.85      66     WidgetA    0.58      60
05-13-2021  AY       WidgetZ   0.15      87     
05-12-2021  AY       WidgetZ   0.15      88     

How can I sort these so that I will get O1 (option 1) Name, Price and Quantity sorted as my data seems to jump sometimes and they don't remain the same column.我如何对这些进行排序,以便我得到 O1(选项 1)名称、价格和数量排序,因为我的数据有时似乎会跳跃并且它们不会保持在同一列。 Please note that O3 O4 also exists and may or may not exist in the dataset (blank)请注意,O3 O4 也存在,可能存在也可能不存在于数据集中(空白)

Forgot to add: Sorting is based on O1_Name O2_Name O3_Name with the Price and Qty following it忘记添加:排序是基于 O1_Name O2_Name O3_Name 以及其后的价格和数量

RESULT REQUIRED:结果要求:

    Date    Product  O1_Name  O1_Price  O1_Qty  O2_Name  O2_Price  O2_Qty
05-12-2021  MK       Widget1   0.99      23     Widget2    1.50      80
05-12-2021  MK       Widget1   0.99      25     Widget2    1.50      85
05-11-2021  MK       Widget1   0.99      28     Widget2    1.50      85
05-10-2021  MK       Widget1   0.99      31     Widget2    1.50      95
05-13-2021  PS       WidgetA   0.52      49     WidgetB    0.86      43
05-12-2021  PS       WidgetA   0.52      53     WidgetB    0.86      43
05-10-2021  PS       WidgetA   0.58      60     WidgetB    0.85      66
05-13-2021  AY       WidgetZ   0.15      87     
05-12-2021  AY       WidgetZ   0.15      88   

水平排序

If you want to sort individually, try:如果要单独排序,请尝试:

df.sort_values(by=['O1_Name'])  # or 'O1_Price'

If you would like to sort by all three together, you can:如果您想按所有三个一起排序,您可以:

df.sort_values(by=['O1_Name', 'O1_Price', 'O1_Qty'])

note that O1_Name will be the first importance for the sorting, and O1_Qty least important.请注意,O1_Name 将是排序的第一个重要性,而 O1_Qty 最不重要。

You can do some fancy reshaping sorting and reshaping again:您可以再次进行一些花哨的整形排序和整形:

df.columns = [('_').join(x) for x in df.columns.str.split('_').str[::-1]]

dfm = pd.wide_to_long(df.reset_index(), 
                ['Name', 'Price', 'Qty'], 
                i=['index', 'Date', 'Product'], 
                j='No',
                sep='_',
                suffix='.*')

dfm = dfm.sort_values(['index', 'Date', 'Product', 'Price'])\
   .reset_index('No', drop=True)

dfm = dfm.set_index('O' + (dfm.groupby(['index', 'Date', 'Product']).cumcount()+1).astype(str), 
                    append=True)

dfm  = dfm.unstack().sort_index(level=1, axis=1)
dfm.columns = dfm.columns.map('_'.join)
df_out = dfm.reset_index()
print(df_out)

Output:输出:

   index        Date Product  Name_O1  Price_O1  Qty_O1  Name_O2  Price_O2  Qty_O2
0      0  05-12-2021      MK  Widget1      0.99    23.0  Widget2      1.50    80.0
1      1  05-12-2021      MK  Widget1      0.99    25.0  Widget2      1.50    85.0
2      2  05-11-2021      MK  Widget1      0.99    28.0  Widget2      1.50    85.0
3      3  05-10-2021      MK  Widget1      0.99    31.0  Widget2      1.50    95.0
4      4  05-13-2021      PS  WidgetA      0.52    49.0  WidgetB      0.86    43.0
5      5  05-12-2021      PS  WidgetA      0.52    53.0  WidgetB      0.86    43.0
6      6  05-10-2021      PS  WidgetA      0.58    60.0  WidgetB      0.85    66.0
7      7  05-13-2021      AY  WidgetZ      0.15    87.0     None       NaN     NaN
8      8  05-12-2021      AY  WidgetZ      0.15    88.0     None       NaN     NaN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 pandas 中使用 groupby 按 bin 对数据进行排序? - How can I sort data by bins using groupby in pandas? 我可以使用 groupby 在 Pandas 数据框中创建每行都是运行列表的列吗? - Can I create column where each row is a running list in a Pandas data frame using groupby? 如何在Pandas数据框中按行值对日期时间列进行排序? - How can I sort datetime columns by row value in a Pandas dataframe? 如何将每个 Pandas Data Frame 行转换为包含列值作为属性的对象? - How can I convert each Pandas Data Frame row into an object including the column values as the attributes? 我如何使用 python 中的 pandas 遍历每一行中的所有列 - How can i loops throught all column in each row using pandas in python 在熊猫中使用groupby后,如何获得每个组的第一行? - How can I get first row of each group after using groupby in pandas? 如何将 function 应用于 pandas dataframe 中的每一行? - How can I apply a function to each row in a pandas dataframe? 使用 Pandas,如何删除每组的最后一行? - Using Pandas, how do I drop the last row of each group? 如何使用Pandas读取乱序的数据并进行排序? - How can I use Pandas to read in data that is out of order and sort it? 如何按 Pandas 中重新设计的案例组对时间戳数据进行排序? - How can I sort timestamp data by redesigned case groups in Pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM