改组熊猫数据框列

时间:2020-10-15 09:29:05

标签: python pandas dataframe

我需要重新整理数据框列。目前,我是通过这种方式完成的:

import random
import pandas as pd
import numpy

df = pd.DataFrame(numpy.random.rand(1,5))
print (df)
df_as_list = df.values.tolist()[0]
random.shuffle(df_as_list)
df_shuffled = pd.DataFrame(df_as_list).transpose()
print (df_shuffled)

之前:

          0         1         2         3         4
0  0.472918  0.261734  0.987053  0.921826  0.144114

之后:

          0         1         2         3         4
0  0.472918  0.921826  0.987053  0.144114  0.261734

这样就可以了,但是必须有更好的方法来做到这一点。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

一种不同的方式:

import discord
import os
import asyncio
import time

# CHANNEL_ID = 7659170174????????
client = discord.Client()
channel = None

class Example():
    # Imagine this run comes from a subclass, so you can't add sync to it!
    def run(self):
        # await channel.send('Test') # We can't do this because of the above comment
        asyncio.run_coroutine_threadsafe(channel.send('Test'), _loop)
        print('Message sent')

@client.event
async def on_ready():
    print('Discord ready')
    global channel
    channel = client.get_channel(CHANNEL_ID)

    for i in range(2):
        Example().run()
        time.sleep(3)

    print('Discord messages should appear by now. Sleeping for 20s to give it time (technically this would be infinite)')
    time.sleep(20)
    print('Script done. Now they only get sent for some reason')

_loop = asyncio.get_event_loop()

client.run('Your secret token')

答案 1 :(得分:0)

尝试

def shuffle(df, n=1):
    for _ in range(n):
        df.apply(np.random.shuffle)
        return df
df = pd.DataFrame({'A':range(10), 'B':range(10)})

shuffle(df)

print(df)