Python学习笔记:Pandas生成任意行的循环取值数据
一、需求
生成一个数据框,行数不确定,有两列:a-z循环,1-2循环,可根据需要指定数据行数。
二、实操
# Pandas生成任意行的循环取值数据
import pandas as pd
import string
import itertools
string.ascii_letters # 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
string.ascii_lowercase # 'abcdefghijklmnopqrstuvwxyz'
string.ascii_uppercase # 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
col1 = itertools.cycle(string.ascii_lowercase)
col2 = itertools.cycle([1, 2])
n = 1000
df = pd.DataFrame({
'col1': [next(col1) for _ in range(n)],
'col2': [next(col2) for _ in range(n)]
})
参考链接:pandas 生成任意行循环取值数据