将零数组添加到Pandas Dataframe中

时间:2021-12-07 22:56:14

I'm looking for code to add [0,0] arrays to the 'Mar' column if the shape is smaller than (3,)

如果形状小于(3,),我正在寻找将[0,0]数组添加到'Mar'列的代码

Sample Dataframe visualized:

示例数据框可视化:

 df:
  account        Jan      Feb          Mar
Jones LLC  |     150   |   200    | [.332, .326], [.058, .138]
Alpha Co   |     200   |   210    | [[.234, .246], [.234, .395], [.013, .592]]
Blue Inc   |     50    |   90     | [[.084, .23], [.745, .923]]

Again: I'm looking for code to add [0,0] arrays to the 'Mar' column so that any row with shape smaller than (3,x) is modified, resulting in the following df

再说一遍:我正在寻找将[0,0]数组添加到'Mar'列的代码,以便修改形状小于(3,x)的任何行,从而得到以下df

 df:
  account        Jan      Feb          Mar
Jones LLC  |     150   |   200    | [.332, .326], [.058, .138], [0, 0]
Alpha Co   |     200   |   210    | [[.234, .246], [.234, .395],[.013, .592] 
Blue Inc   |     50    |   90     | [[.084, .23], [.745, .923], [0, 0]

Code to create sample dataframe:

用于创建示例数据帧的代码:

Sample = [{'account': 'Jones LLC', 'Jan': 150, 'Feb': 200, 'Mar': [[.332, .326], [.058, .138]]},
     {'account': 'Alpha Co',  'Jan': 200, 'Feb': 210, 'Mar': [[.234, .246], [.234, .395], [.013, .592]]},
     {'account': 'Blue Inc',  'Jan': 50,  'Feb': 90,  'Mar': [[.084, .23], [.745, .923]]}]
df = pd.DataFrame(Sample)

1 个解决方案

#1


1  

You could add extra values,

你可以添加额外的值,

df['Mar'] = df['Mar']+[[0, 0], [0, 0], [0, 0]]

then trim it down, using a method from here.

然后使用此处的方法将其修剪下来。

#1


1  

You could add extra values,

你可以添加额外的值,

df['Mar'] = df['Mar']+[[0, 0], [0, 0], [0, 0]]

then trim it down, using a method from here.

然后使用此处的方法将其修剪下来。