簡體   English   中英

熊貓:合並系列值

[英]Pandas: merging Series values

我有一個Pandas數據幀,如下所示。 如何將shape系列下的roundsquare值合並為other (在R術語中,我想將shape因子的roundsquare水平合並到標記為other的新水平。)

df = pd.DataFrame({'id' : range(1,9),
                    'code' : ['one', 'one', 'two', 'three',
                                'two', 'three', 'one', 'two'],
                    'shape': ['round', 'triangular', 'triangular','triangular','square',
                                        'triangular','round','triangular'],
                    'amount' : np.random.randn(8)},  columns= ['id','code', 'shape', 'amount'])
df 
   id   code       shape    amount
0   1    one       round -0.187789
1   2    one  triangular  1.286208
2   3    two  triangular  0.171734
3   4  three  triangular  0.394471
4   5    two      square -0.009613
5   6  three  triangular  0.413767
6   7    one       round  1.264730
7   8    two  triangular  0.516499

你是這個意思嗎?

df.loc[df['shape'].isin(['round', 'square']), 'shape'] = 'other'

(根據@ TomAugspurger的建議編輯)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM