pandas create a series with n elements (sequential or randbetween)

流过昼夜 提交于 2019-12-18 22:32:04

问题


I am trying to create a pandas series.

One column of the series should contain n sequential numbers. [1, 2, 3, ..., n]

One column should contain random numbers between k and k+100.

One column should contain random selection between strings in a list. ['A', 'B', 'C', ... 'Z']


回答1:


There can be a lot of solutions. In the comments of the code block (#) you will find a few links for more information:

import pandas as pd
import numpy as np
import random
import string

k = 5
N = 10

#http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.randint.html
#http://stackoverflow.com/a/2257449/2901002

df = pd.DataFrame({ 'A' : range(1, N + 1 ,1),
    'B' : np.random.randint(k, k + 100 , size=N),
    'C' : pd.Series(random.choice(string.ascii_uppercase) for _ in range(N)) })

print df
#    A   B  C
#0   1  60  O
#1   2  94  L
#2   3  10  W
#3   4  94  X
#4   5  60  O
#5   6  20  K
#6   7  58  Y
#7   8  40  I
#8   9  49  X
#9  10  65  S

Numpy solution:

import pandas as pd
import numpy as np

k = 5
N = 10

alphabet = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ')

#http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.choice.html

df = pd.DataFrame({ 'A' : range(1, N + 1 ,1),
    'B' : np.random.randint(k, k + 100 , size=N),
    'C' : np.random.choice(np.array(alphabet, dtype="|S1"), N) })

print df
#    A    B  C
#0   1   16  U
#1   2   76  X
#2   3  101  N
#3   4   61  F
#4   5   52  J
#5   6   62  A
#6   7   99  L
#7   8   23  N
#8   9   75  D
#9  10   16  Q



回答2:


import pandas
n = 30
k = 40
pandas.DataFrame([(i, random.randint(k, k+100), chr(random.randint(ord('A'), ord('Z')))) for i in xrange(0, n)

If you want you specify the column names otherwise it is set to 0,1,2



来源:https://stackoverflow.com/questions/33828475/pandas-create-a-series-with-n-elements-sequential-or-randbetween

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!