简体   繁体   中英

How to append python list to each pandas dataframe cell containing a array?

MY CODE:

df=pd.DataFrame(columns=["a","b"], index=np.arange(1,3))
for col in df.columns:
  df[col].values[:]=[[]*2]
print(df)
for col,n in enumerate(df.columns):
  for row in range(1,3):
    df[n][row].append([1,2,3])
    print(df)

OUTPUT:

           a   b
1  []  []
2  []  []
0 a
             a   b
1  [[1, 2, 3]]  []
2  [[1, 2, 3]]  []
0 a
                        a   b
1  [[1, 2, 3], [1, 2, 3]]  []
2  [[1, 2, 3], [1, 2, 3]]  []
1 b
                        a            b
1  [[1, 2, 3], [1, 2, 3]]  [[1, 2, 3]]
2  [[1, 2, 3], [1, 2, 3]]  [[1, 2, 3]]
1 b
                        a                       b
1  [[1, 2, 3], [1, 2, 3]]  [[1, 2, 3], [1, 2, 3]]
2  [[1, 2, 3], [1, 2, 3]]  [[1, 2, 3], [1, 2, 3]]
   

I want output as the list added to all the cells. But instead the list being added to a cell, it is being added to each row of the column. Please help. Thank you in advance.

Suggested output:

    a       b
1   []     []
2   []     []
0 a
         a    b
1   [[1,2,3]] []
2       []    []
0 a
         a    b
1   [[1,2,3]] []
2   [[1,2,3]] []
1 b
         a         b
1   [[1,2,3]]  [[1,2,3]]
2   [[1,2,3]]  []
1 b
         a         b
1   [[1,2,3]]  [[1,2,3]]
2   [[1,2,3]]  [[1,2,3]]

In Python, doing [[] * 2] doesn't actually produce [[],[]] as you probably expected, but [[]] . When you do df[col].values[:] = [[]*2] you are actually setting all rows in the column col to the same list. You can check this like so:

df = pd.DataFrame(columns=["a","b"], index=np.arange(1,3))
df["a"].values[:] = [[] * 2]
df["a"][1].append([1,2,3])
print(df)

After doing above operation the df would become like below, since you are actually modifying both rows at once:

             a    b
1  [[1, 2, 3]]  NaN
2  [[1, 2, 3]]  NaN

What you want to do is this:

for col in df.columns:
    for row in df.index:
        df[col][row] = []

Then

for col in df.columns:
    for row in df.index:
        df[col][row].append([1,2,3])
        print(df)

(Also notice how I don't use enumerate(df.columns) because we don't need it, and replaced range(1,3) with df.index .)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM