簡體   English   中英

Pandas 數據框沒有附加和缺少計數器

[英]Pandas dataframe doesnot append and missing the counter

我正在嘗試附加到數據幀以獲得如下所示的預期結果。

我如何正確附加它? 目前我在下面的 for 循環中沒有得到正確的數據框請幫忙

代碼

df = pd.DataFrame([])
no = 0
for i in range(1,10):
    no = no + 1
    tt = "ab"
    lst = [no, lst]
    df = df.append(lst)

預期結果

no tt
1  ab
2  ab
3  ab
.  .
.  .

嘗試:

df = pd.DataFrame()
no = 0
for i in range(1,10):
    no = no + 1
    tt = "ab"
    lst = {'no':no,'tt':tt}
    df = df.append(lst,ignore_index=True)

要么

為了更有效,只需簡單地使用pd.DataFrame()

df=pd.DataFrame({'no':range(1,10),'tt':'ab'})

像我一樣在您的提供代碼中進行此修改

df = pd.DataFrame([],columns=['no','tt'])
no = 0
for i in range(1,10):
    no = no + 1
    lst = [no, 'ab']
    df_length = len(df)
    df.loc[df_length] = lst

希望這對你有幫助

暫無
暫無

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

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