简体   繁体   English

按值类型反转 pd.Series 中的值

[英]Reverse values in pd.Series by their value type

I have this pd.Series :我有这个pd.Series

s = pd.Series([1, 'a', 1.4, 'b', 4, 98, 6.7, 'hello', 98.9])

My goal is to switch the values by each value type in reverse order.我的目标是以相反的顺序按每个值类型切换值。

My desired output is:我想要的输出是:

>>> s = pd.Series([98, 'hello', 98.9, 'b', 4, 1, 6.7, 'a', 1.4])
>>> s
0       98
1    hello
2     98.9
3        b
4        4
5        1
6      6.7
7        a
8      1.4
dtype: object
>>> 

As you can see, the different value types are still in mixed order, but they are reversed by the other same type values.正如您所看到的,不同的值类型仍然是混合顺序,但它们被其他相同类型的值颠倒了。

  • The integer order was 1, 4, 98 and it's now 98, 4, 1 .整数顺序是1, 4, 98 ,现在是98, 4, 1

  • The float order was 1.4, 6.7, 98.9 and it's now 98.9, 6.7, 1.4 .浮动订单是1.4, 6.7, 98.9 ,现在是98.9, 6.7, 1.4

  • The string order was 'a', 'b', 'hello' and it's now 'hello', 'b', 'a'字符串顺序是'a', 'b', 'hello'现在是'hello', 'b', 'a'

What I have tried so far is:到目前为止我尝试过的是:

>>> s.to_frame().groupby([[*map(type, s)]], sort=False).apply(lambda x: x.iloc[::-1]).reset_index(drop=True)
       0
0     98
1      4
2      1
3  hello
4      b
5      a
6   98.9
7    6.7
8    1.4
>>> 

And yes, they do get reversed in order.是的,它们的顺序确实颠倒了。 But, since I'm using groupby , the values are grouped together into separated groups, they're not mixed together.但是,由于我使用的是groupby ,所以这些值被分组到不同的组中,它们没有混合在一起。

How would I fix this?我将如何解决这个问题?

out = (s.groupby(s.map(type), sort=False)
       .apply(lambda x: pd.Series(x.sort_values(ascending=False).tolist(), index=x.index)))

out

0       98
1    hello
2     98.9
3        b
4        4
5        1
6      6.7
7        a
8      1.4
dtype: object

I just figured out a solution myself, it's a bit long an inefficient.我只是自己想出了一个解决方案,它有点长而且效率低下。 I would still prefer better solutions though.不过,我仍然更喜欢更好的解决方案。

This is how I did it:我就是这样做的:

print(
     s.to_frame().groupby([[*map(type, s)]], sort=False)
     .apply(lambda x: x.reindex(x.index[::-1])
     .rename(index=dict(zip(x.index[::-1], x.index))))
     .reset_index(level=0, drop=True)
     .sort_index().squeeze().rename(index=None)
)

Output:输出:

0       98
1    hello
2     98.9
3        b
4        4
5        1
6      6.7
7        a
8      1.4
dtype: object

I use to_frame to convert this Series into a DataFrame , and I use map to give me the types of all the values.我使用to_frame将此Series转换为DataFrame ,并使用map为我提供所有值的类型。 After that, I use groupby to group them and apply and rename to reverse the order/index of them.之后,我使用groupby对它们进行分组并applyrename以反转它们的顺序/索引。 The next step is to use reset_index to reset the index and sort the values by the index using sort_index .下一步是使用reset_index重置索引并使用sort_index按索引对值进行排序。 Then, I just use squeeze to convert this DataFrame to a Series again, and rename to remove the index name.然后,我再次使用squeeze将这个DataFrame转换为Seriesrename以删除索引名称。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM