python - 如何调整行或列中的具体数据?

标签 python pandas numpy matplotlib dataframe

我使用嵌套循环有点奇怪,但它失败了,它显示“numpy.ndarray”对象不可调用。那么,有没有更好的方法来调整列数据或行中的每个数据呢? 例如:

df

   a  b  c  d
A  1  3  4  7
B  2  3  5  1
C  1  5  6  8
D  2  1  7  9

如何添加数字,例如

num = 1

我想将 num 添加到“c”列中的每个数据,如下所示:

   a  b  c  d
A  1  3  5  7
B  2  3  6  1
C  1  5  7  8
D  2  1  8  9 

或者我想将 num 添加到“C”行中的每个数据,如下所示:

   a  b  c  d
A  1  3  4  7
B  2  3  5  1
C  2  6  8  9
D  2  1  7  9 

这怎么行?使用嵌套循环,例如

for row in df.index:
    for row in df['c']

或者

for row in df.index:
    df.ix[row, 'c'] = df.ix[row, 'c'] + num

有人可以帮我看看吗?谢谢!!!

最佳答案

df.loc[:, 'c'] += 1
df.loc['C', :] += 1

关于python - 如何调整行或列中的具体数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40391638/

相关文章:

python - 使用索引列表从 2d 数组获取 1d numpy 数组

python - 如何用zeep正确生成xml?

python - 在两个给定点之间画线(OpenCV,Python)

python - 通过 Python Launcher 调用 pip

python - Google App Engine with/Django - InboundeMailHandler 似乎只工作一次

Python pandas : insert rows for missing dates, groupby 数据帧中的时间序列

python - 嵌入python+numpy时延迟加载python DLL

python - 无法创建第二个数据框 python pandas

python - 如何测试 pandas.Series 是否只包含特定类型(例如 int)?

python - 将值列表分配给 numpy 数组