如何将现有pandas Dataframe的值相乘?

时间:2017-07-13 17:11:39

标签: python python-3.x pandas

我有一个来自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()

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

您无法将.mul()用于列表。我认为您在[' W']之前缺少数据框名称,添加数据框名称并且.mul函数正常工作,如果geyser_June_e2是数据框名称,那么

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()