Numpy .shuffle每次给出相同的结果

时间:2016-08-09 22:53:21

标签: python pandas numpy random

我正在尝试使用pandas DataFrame,取出1列,随机播放该列的内容,然后将其放回DataFrame并返回。这是使用的代码:

def randomize(self, data, column):
    '''Takes in a pandas database and randomizes the values in column.

    data is the pandas dataframe to be altered.
    column is the column in the dataframe to be randomized.

    returns the altered dataframe.
    '''
    df1 = data
    df1.drop(column, 1)
    newcol = list(data[column])
    np.random.shuffle(newcol)
    df1[column] = newcol
    return df1

每次运行时都会输出相同的输出。那是为什么?

注意:我每次都使用相同的数据框。

1 个答案:

答案 0 :(得分:1)

您的代码

<EditFormSettings EditFormType="Template">
                        <EditColumn UniqueName="EditCommandColumn1" FilterControlAltText="Filter EditCommandColumn1 column">
                        </EditColumn>
                        <FormTemplate>
                            <div class="FinancePositioncontainers">
                                <div class="FinancePosition-container" id="decorationZone">
                                    <h4>Finance</h4>
                                    <fieldset>
                                        <legend>List</legend>
                                        <div class="formRow" style="padding-right: 10px; padding-left: 10px;">
                                            <h5>Bal</h5>
                                            <label for="txt_CashAndEquivalents">bal1:</label>
                                            <telerik:RadNumericTextBox ID="txt_CashAndEquivalents" runat="server" Text='<%# Eval("CashAndEquivalents")%>' Type="Number" DataType="System.Decimal" Width="160" NumberFormat-GroupSizes="3" AutoPostBack="True" TabIndex="1" OnTextChanged="txt_CashAndEquivalents_TextChanged"></telerik:RadNumericTextBox>

                                            <div>
                                                &nbsp;
                                            </div>
                                            <label for="txt_ShortTermInvestments">bal2:</label>
                                            <telerik:RadNumericTextBox ID="txt_ShortTermInvestments" runat="server" Text='<%# Eval("ShortTermInvestments")%>' Type="Number" DataType="System.Decimal" Width="160" NumberFormat-GroupSizes="3" TabIndex="2" AutoPostBack="True" OnTextChanged="txt_CashAndEquivalents_TextChanged"></telerik:RadNumericTextBox>

                                            <div>
                                                &nbsp;
                                            </div>

                                            <hr />
                                            <label for="txt_OneTotal">total  :</label>
                                            <telerik:RadNumericTextBox ID="txt_OneTotal" runat="server" ReadOnly="true" Type="Number" DataType="System.Decimal" Width="160" NumberFormat-GroupSizes="3"></telerik:RadNumericTextBox>

                                            <div>
                                                &nbsp;
                                            </div>
                                        </div>

                                    <asp:LinkButton ID="btnUpdate" runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'
                                        ValidationGroup="Main" CssClass="btn btn-primary btn-xs white_cr"><span class="glyphicon glyphicon-floppy-disk"></span> <%# (Container is GridEditFormInsertItem) ? "Uppdate" %>
                                    </asp:LinkButton>&nbsp;
                                        <asp:LinkButton ID="btnCancel" runat="server" CausesValidation="False" CommandName="Cancel"
                                            CssClass="btn btn-primary btn-xs white_cr"><span class="glyphicon glyphicon-ban-circle"></span>Cancel</asp:LinkButton>
                                    <br />
                                    <br />
                                </div>

                            </div>

                        </FormTemplate>
                    </EditFormSettings>

我的def randomize(data, column): df1 = data.copy() newcol = list(data[column]) np.random.shuffle(newcol) df1[column] = newcol return df1

df

您的代码+我的df = pd.DataFrame(np.arange(25).reshape(5, 5), list('abcde'), list('ABCDE'))

df

enter image description here

再次

np.random.seed([3,1415])
randomize(df, 'A')

enter image description here

看起来很有效!