Возьмите повторяющиеся образцы и добавьте их в новый фрейм данных: Python Pandas

#python #pandas #dataframe

#python #pandas #фрейм данных

Вопрос:

Я хочу взять повторяющиеся выборки из фрейма данных. У меня есть следующий код:

 df = pd.read_csv(filename) #Any data set would work, e.g, iris
new_df = pd.DataFrame(columns = df.columns, index = df.index) 
#not specifying the columns and index gives me an empty dataframe 
    
for i in range(20): #I want to take sample from the dataframe 20 times. 
    new_df.append(df.sample(n = 10, random_state = i), ignore_index = True)
        
        
    print(new_df) # This prints out columns with NaN values. Not sure what to do. 
 

Ответ №1:

Вам нужно назначить возврат из добавления в new_df:

 df = pd.read_csv(filename) 
new_df = pd.DataFrame(columns = df.columns, index = df.index) 

for i in range(20): 
    new_df = new_df.append(df.sample(n = 10, random_state = i), ignore_index = True)
print(new_df)