如何在python中初始化二维字符串DataFrame数组

时间:2017-09-21 13:42:11

标签: python arrays pandas spyder

我想初始化31756x2字符串数据框。 我希望它看起来像这样:

    Array ( 
[free_shipping:5] => WC_Shipping_Rate Object ( [id] => free_shipping:5 [label] => International Free [cost] => 0.00 [taxes] => Array ( ) [method_id] => free_shipping [meta_data:WC_Shipping_Rate:private] => Array ( [Items] => Portsea Polo - 2018 × 1 ) ) 

[flat_rate:4] => WC_Shipping_Rate Object ( [id] => flat_rate:4 [label] => International Regular [cost] => 34 [taxes] => Array ( ) [method_id] => flat_rate [meta_data:WC_Shipping_Rate:private] => Array ( [Items] => Portsea Polo - 2018 × 1 ) ) 

[per_product] => WC_Shipping_Rate Object ( [id] => per_product [label] => Express shipping [cost] => 9.00 [taxes] => Array ( ) [method_id] => per_product [meta_data:WC_Shipping_Rate:private] => Array ( ) ) 
)

我写道:

index  column1     column2

0        A           B
1        A           B
.
.
31756    A           B

结果如下:

enter image description here

我确实得到了一个二维列表,但我希望列在数据框中分开,我似乎无法使它工作(如column1:A ..,column2:B。 ..)

会喜欢一些帮助。

4 个答案:

答案 0 :(得分:2)

仅使用DataFrame构造函数:

df = pd.DataFrame([["A", "B"] for x in range(31756)], columns=['col1','col2'])
print (df.head())

  col1 col2
0    A    B
1    A    B
2    A    B
3    A    B
4    A    B

或者:

N = 31756
df = pd.DataFrame({'col1':['A'] * N, 'col2':['B'] * N})
print (df.head())
  col1 col2
0    A    B
1    A    B
2    A    B
3    A    B
4    A    B

答案 1 :(得分:2)

使用numpy.tile

import numpy as np
df = pd.DataFrame(np.tile(list('AB'), (31756, 1)), columns=['col1','col2'])

或者只是传递字典:

df = pd.DataFrame({'A':['A']*31756, 'B':['B']*31756})

如果使用后一种方法,您可能希望显式对列进行排序,因为字典没有顺序:

df = pd.DataFrame({'A':['A']*31756, 'B':['B']*31756}).sort_index(axis=1)

答案 2 :(得分:2)

import pandas as pd

df = pd.DataFrame(index=range(31756))
df.loc[:,'column1'] =  'A'
df.loc[:,'column2'] = 'B'

答案 3 :(得分:2)

为了好玩

pd.DataFrame(index=range(31756)).assign(dict(col1='A', col2='B'))