简体   繁体   English

如何写入现有excel文件的B列

[英]How can I write to B column of an existing excel file

I am completely new to programming and I have a problem like every beginner I guess:).我对编程完全陌生,我想每个初学者都有一个问题:)。 So basically I am trying to convert/calculate some numbers using this formula: (((prices*1.1)/8.6)+1+3.75+1)/0.76 and I could convert every number (there are like 1000) with this code所以基本上我想用这个公式来转换/计算一些数字:(((prices*1.1)/8.6)+1+3.75+1)/0.76 我可以用这个代码转换每个数字(比如 1000)

import pandas as pd
data = pd.read_excel(r"C:\\Users\\bbura\\Downloads\\test1.xlsx")
list = data["TL Price"].tolist()
for prices in list:
  result = (((prices*1.1)/8.6)+1+3.75+1)/0.76
  print(result)

I need to write these new prices into column B of test1.xlsx file and I have no idea how to do that.我需要将这些新价格写入 test1.xlsx 文件的 B 列中,但我不知道该怎么做。

Thanks for your help and please forgive my bad english.感谢您的帮助,请原谅我的英语不好。

Alignment to your code you can do :与您的代码对齐,您可以这样做:

data = pd.read_excel(r"C:\\Users\\bbura\\Downloads\\test1.xlsx")
list_ = data["TL Price"].tolist()
B_col = []
for prices in list:
   result = (((prices*1.1)/8.6)+1+3.75+1)/0.76
   B_col.append(result)

data['B'] = B_col

But this involves lot of lines.但这涉及很多行。 One liner by @Myrt is more useful. @Myrt 的一个班轮更有用。 Also, you are using list as variable name, which is conflicting as list is predefined keyword in python.此外,您使用 list 作为变量名,这是冲突的,因为 list 是 python 中的预定义关键字。

您可能想使用应用功能

data['B']=data['TL Price'].apply(lambda x: (((x*1.1)/8.6)+1+3.75+1)/0.76)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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