【问题标题】:change all pandas dataframe cell in a column with an array使用数组更改列中的所有熊猫数据框单元格
【发布时间】:2019-11-19 19:38:54
【问题描述】:

我有一个如下的熊猫数据框:

                   column1         column2
0                     0               0
1                     0               0
2                     0               0
3                     0               0
...                  ...             ...

我想用

更改 column2 中的所有数据
a = numpy.zeros([16,16,16])

所以数据框看起来像

                   column1         column2
0                     0           [[[0,0,0,....
1                     0           [[[0,0,0,....
2                     0           [[[0,0,0,....
3                     0           [[[0,0,0,....
...                  ...             ...

【问题讨论】:

  • df['col'] = [np.zeros([16,16,16])]*len(df)
  • 一开始为什么要这样做?您也可以保留 numpy 数组并相应地对其进行索引,就像您对数据框所做的那样

标签: python pandas list numpy


【解决方案1】:

首先,我认为在 pandas 中使用 lists 或 array 的这种方式不是 good idea

但有可能:

df['column2'] = [a for _ in df.index]

【讨论】:

  • 谢谢,我使用的旧数据框完全使用该格式,更改所有内容非常麻烦,所以我宁愿使用该格式进行更改。
猜你喜欢
  • 2017-01-16
  • 2019-02-13
  • 2022-01-12
  • 2018-01-24
  • 2022-12-05
  • 1970-01-01
  • 2021-05-31
  • 2020-08-06
  • 1970-01-01
相关资源
最近更新 更多