如何编辑数据框的输出?

时间:2018-10-08 14:53:27

标签: python pandas pandas-groupby

import csv
import pandas as pd
data = {'numbers' : [1, 2, 3, 1, 8, 4, 5, 7, 3, 6, 2, 7, 4, 5, 8]
'colors' : ['red', 'yellow', 'orange', 'green', 'blue', 'purple', 'magenta', 'grey', 'pink', 'cyan', 'lime', 'apricot', 'teal', 'navy', 'maroon']}

df = pd.DataFrame(data, columns = ['numbers', 'colors'])    
temp = df.groupby(['numbers'])
temp1 = temp.sum()['colors']
print(temp1)

当前,我的输出如下:

1 redgreen
2 yellowlime
3 orangepink

我希望能够用箭头格式化输出:

1 red --> green
2 yellow --> lime
3 orange --> pink

它还应该能够使用3个或更多值,例如:

10 value1 --> value2 --> value3 --> value4

我将不胜感激,谢谢!

3 个答案:

答案 0 :(得分:4)

您可以在一行中完成

df.groupby('numbers').agg({'colors': lambda x: ' --> '.join(x)})

如果打印出来,您将得到:

                   colors
numbers                  
1           red --> green
2         yellow --> lime
3         orange --> pink
4         purple --> teal
5        magenta --> navy
6                    cyan
7        grey --> apricot
8         blue --> maroon

更简单,感谢@piRSquared

df.groupby('numbers').colors.apply(' --> '.join)

答案 1 :(得分:3)

IIUC,您仍然可以使用sum

(df.colors+'-->').groupby(df.numbers).sum().str[:-3]
Out[488]: 
numbers
1       red-->green
2     yellow-->lime
3     orange-->pink
4     purple-->teal
5    magenta-->navy
6              cyan
7    grey-->apricot
8     blue-->maroon
Name: colors, dtype: object

答案 2 :(得分:0)

仅使用带有itertools.groupbyoperator.itemgetter的python /可以替换为lamba

from itertools import groupby
from operator import itemgetter

lst = [v for v in data.values()]
z = zip(lst[0], lst[1])
z = sorted(z, key=itemgetter(0))

for k, g in groupby(z, key=itemgetter(0)):
    x = list(g)
    if len(x) > 1:
        print('{} {:<7} --> {}'.format(k, x[0][1], x[1][1]))
    else:
        print('{} {}'.format(k, x[0][1]))
1 red     --> green
2 yellow  --> lime
3 orange  --> pink
4 purple  --> teal
5 magenta --> navy
6 cyan
7 grey    --> apricot
8 blue    --> maroon