问题
I have a Pandas aggregate dataframe like this:
import pandas as pd
agg_df = pd.DataFrame({'v1':['item', 'item', 'item', 'item', 'location', 'status', 'status'],
'v2' :['bed', 'lamp', 'candle', 'chair', 'home', 'new', 'used' ],
'count':['2', '2', '2', '1', '7', '4', '3' ]})
agg_df
I want to prepare it for academic publication and I need a new dataframe like this:
# item bed 2
# lamp 2
# candle 2
# chair 1
# location home 7
# status new 4
# used 3
How can I create such a dataframe?
回答1:
For display only is possible use MultiIndex
:
df = agg_df.set_index(['v1','v2'])
print (df)
count
v1 v2
item bed 2
lamp 2
candle 2
chair 1
location home 7
status new 4
used 3
If need replace duplicated values use Series.duplicated with Series.mask:
agg_df['v1'] = agg_df['v1'].mask(agg_df['v1'].duplicated(),'')
print (agg_df)
v1 v2 count
0 item bed 2
1 lamp 2
2 candle 2
3 chair 1
4 location home 7
5 status new 4
6 used 3
If need remove index and columns values:
print (agg_df.to_string(index=False, header=None))
item bed 2
lamp 2
candle 2
chair 1
location home 7
status new 4
used 3
回答2:
u can do that using
import pandas as pd
agg_df = pd.DataFrame({'v1':['item', 'item', 'item', 'item', 'location', 'status', 'status'],
'v2' :['bed', 'lamp', 'candle', 'chair', 'home', 'new', 'used' ],
'count':['2', '2', '2', '1', '7', '4', '3' ]})
agg_df.set_index(["v1","v2"])
来源:https://stackoverflow.com/questions/64318422/preparing-an-aggregate-dataframe-for-publication