用每行增加的列偏移填充大熊猫数据框

时间:2018-06-21 22:40:18

标签: python pandas numpy dataframe

我正在尝试创建一个熊猫数据框,其中每一行都填充有相同的一组值,但是每行的起始列对于在数据框中向下迭代的每一行都增加1。这是我当前的方法,也是我希望获得的数据框示例。我在这里做什么错了?

谢谢!

import pandas as pd
import numpy as np

df_to_fill = pd.DataFrame(data=np.zeros((4,6)),columns=[1,2,3,4,5,6])

values_to_fill = pd.Series(np.arange(1,4))

# vars to iterate over dataframe
num_rows = len(df_to_fill)
# counter to keep track of how many columns to offset from to fill dataframe in loop below
col_offset = 0

for row in range(0,num_rows):
    # Fill the first row from first column onwards, 2nd from 2nd column onwards,...
    df_to_fill.iloc[row,col_offset:] = values_to_fill
    # Fill the remaining columns in the row with the last value from the values to fill series
    df_to_fill.iloc[row,:].fillna(values_to_fill.values[-1],inplace=True)    
    col_offset += 1

offset_array = np.array([[1,2,3,3,3,3],[0,1,2,3,3,3],[0,0,1,2,3,3]])
desired_df = pd.DataFrame(data=offset_array,columns=[1,2,3,4,5,6])

2 个答案:

答案 0 :(得分:1)

您可以使用shift + concat

创建与第一行相同的填充值
values_to_fill = pd.Series(np.arange(1,4),index=np.arange(1,4)).reindex(np.arange(1,7)).ffill().astype(int)

pd.concat([values_to_fill.shift(x) for x in range(num_rows)],axis = 1 ).T.fillna(0)
     1    2    3    4    5    6
0  1.0  2.0  3.0  3.0  3.0  3.0
1  0.0  1.0  2.0  3.0  3.0  3.0
2  0.0  0.0  1.0  2.0  3.0  3.0
3  0.0  0.0  0.0  1.0  2.0  3.0

答案 1 :(得分:1)

使用列表理解:

A = [1, 2, 3, 3, 3, 3]

res = pd.DataFrame([A] + [[0]*i + A[:-i] for i in range(1, 3)],
                   columns=range(1, 7))

print(res)

   1  2  3  4  5  6
0  1  2  3  3  3  3
1  0  1  2  3  3  3
2  0  0  1  2  3  3