pandas列中的Python枚举/行计数器 [英] Python enumeration/row counter in a pandas column
问题描述
你好,stackoverflowers
Hello fellow stackoverflowers,
关于Python(pandas/numpy)问题的社区新手.
New to the community with a Python (pandas/numpy) question.
我正在使用pandas和numpy创建一个示例数据框进行测试.但是,由于多种原因,如果需要的话,我需要一个列作为枚举数或行计数器.我已经尝试了枚举功能:
I'm using pandas and numpy to create a sample dataframe for testing. However, for several reasons, I need one of the colums to be an enumerator, or row counter, if you will. I've tried the enumerate function:
import pandas as pd
import numpy as np
N = 100
sample_data = pd.DataFrame({
'A': np.random.rand(N),
'B': enumerate('A',1)})
但我一直收到此错误: TypeError:类型为"enumerate"的对象没有len()
but I keep getting this error: TypeError: object of type 'enumerate' has no len()
我该如何以最有效的方式做到这一点?
How do I go about doing this in the most efficient way possible?
谢谢!
推荐答案
N = 10
第一步,您可以使用range
:
sample_data = pd.DataFrame({
'A': np.random.rand(N),
'B' : range(1, N + 1)}
)
print(sample_data)
A B
0 0.037303 1
1 0.693972 2
2 0.725926 3
3 0.110817 4
4 0.889411 5
5 0.138220 6
6 0.738190 7
7 0.695298 8
8 0.912171 9
9 0.601390 10
您也可以使用enumerate
,但是您需要重新排列列:
You can use enumerate
as well, but you'll need to re-arrange the columns:
sample_data = pd.DataFrame(list(enumerate(np.random.rand(N), 1)),
columns=['B', 'A'])[['A', 'B']]
print(sample_data)
A B
0 0.431247 1
1 0.004129 2
2 0.321802 3
3 0.866617 4
4 0.805049 5
5 0.767841 6
6 0.677050 7
7 0.293936 8
8 0.923059 9
9 0.953954 10
作为替代方法,为什么不只使用构造函数自动创建的索引呢?
As an alternative, why not just use the index that the constructor automatically creates?
sample_data = pd.DataFrame({
'A': np.random.rand(N)})
sample_data['B'] = sample_data.index + 1
print(sample_data)
A B
0 0.117788 1
1 0.177268 2
2 0.762664 3
3 0.667486 4
4 0.531079 5
5 0.291939 6
6 0.066751 7
7 0.497935 8
8 0.883126 9
9 0.598048 10
这篇关于pandas列中的Python枚举/行计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!