簡體   English   中英

使用Pandas將數據寫入python中的特定列

[英]Writing data to specific column in python using pandas

我有一個Excel工作表 ,其中某些列的uniqueidentifier設置為“是”,其余部分為空白。

使用Pandas庫 ,我使用以下命令獲取uniqueidentifier沒有值的數據:

df=pandas.read_excel("sample.xlsx")
df=df[df.uniqueidentifier != "yes"]

然后,我每次循環瀏覽excel文件中的索引時都想在uniqueidenfier列中寫“ yes”。

list=[]
for i in df.index
  list.append(df['Subject'][i])
  # do some function here
  var="some value"

每次我在listvar中獲得相應索引的正確值時。 我希望我的函數在循環中為i值向uniqueidentifier列中寫入一些內容(例如,如果在第4行中,則未設置uniqueidentifier時,列表應從主題中獲取值並追加並寫入“是”到uniqueidentifier列)

除了上述內容,我想將var值寫入excel文件中的相鄰列。

當我剛接觸python並被困在這里時,有人可以幫我嗎?

要將唯一標識符設置為是,可以將以下行添加到循環中:

if [#some conditions are met]:    
    df.loc[i,'uniqueidentifier']='YES'
    df.iloc[i].new_column=var
    #new_column is the name of the column where you write your var value. you can name it however you want.

那么您需要將其導出回excel。 您可以使用:

writer = pd.ExcelWriter('file.xlsx')
df.to_excel(writer,'Sheet1')
writer.save()

但請記住,這將覆蓋您的舊版Excel

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM