2014-12-05 67 views
1

我有一個熊貓數據框 - 一列有航空公司名稱(或公司名稱)。 我想通過將名稱的一小部分(僅在一列中)更改爲相似但不相同的名稱來生成「混亂」數據集。 因此聯合航空公司將成爲UNITED AIRLINES的一員。 以下是我的數據的一個例子設定在熊貓中隨機更改行

Description 
0 United Airlines 
1 Pinnacle Airlines Inc. 
2 Ryanair 
3 British Airways 

反正有由行隨機應用刺的變化爲大熊貓數據幀。 有沒有人有任何想法?

回答

1

您可以使用numpy.random.choice來回報您的索引的隨機選擇,這需要1-d數組,並返回您傳遞大小的隨機選擇:

In [177]: 

rand_indices = np.random.choice(df.index, 2) 
rand_indices.sort() 
rand_indices 
Out[177]: 
array([1, 2], dtype=int64) 
In [178]: 

df.loc[rand_indices] 
Out[178]: 
       Description a 
1 Pinnacle Airlines Inc. 1 
2     Ryanair 2 
In [179]: 

def scramble_text(df, index, col): 
    df.loc[index, col] = df[col].str.upper() 

scramble_text(df, rand_indices, 'Description') 
df 
Out[179]: 
       Description a 
0   United Airlines 0 
1 PINNACLE AIRLINES INC. 1 
2     RYANAIR 2 
3   British Airways 3 
+0

感謝,這正是我之後。我需要更好地學習df.loc函數:) – 2014-12-06 13:37:21