如何对现有数据帧的值进行乘法?

2024-09-28 01:30:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个来自csv文件的数据帧,我绘制了它。你知道吗

它看起来像这样:enter image description here

我想将'W'列的值乘以1000,以便使值为1600而不是1.6。如何在代码中实现这一点?你知道吗

我正在尝试mul函数,但它不起作用:

xcolumn_list1 = ['W']
xcolumn_list1.mul(1000)
geyser_June_e2[xcolumn_list1].plot()
plt.show()

Tags: 文件csv数据函数代码plotshow绘制
2条回答
df=pd.DataFrame({"W":[1,2,3,4,5]})

df*=1000

print(df)

      W
0  1000
1  2000
2  3000
3  4000
4  5000

或者

df=pd.DataFrame({"W":[1,2,3,4,5]})

df.loc[:,"W"]=df.loc[:,"W"]*1000

print(df)

      W
0  1000
1  2000
2  3000
3  4000
4  5000

如果您有多列并且只想将某些列的值相乘

不能对列表使用.mul()。我认为在['W']之前缺少了dataframe名称,添加dataframe名称,.mul函数可以正常工作,即如果geyser_June_e2是dataframe名称,那么

xcolumn_list1 = geyser_June_e2['W']  
xcolumn_list1.mul(1000) 

在你的例子中,你想要绘制数据,就像John Galt说的那样,你可以直接乘以1000,然后绘制数据

xcolumn_list1 = ['W']
geyser_June_e2[xcolumn_list1] *= 1000
geyser_June_e2[xcolumn_list1].plot()
plt.show() 

相关问题 更多 >

    热门问题