第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Pandas 未在索引分配 Python3.5.0 后添加列

Pandas 未在索引分配 Python3.5.0 后添加列

桃花長(zhǎng)相依 2022-07-26 16:00:15
我嘗試了以下陳述:import pandas as pddf = pd.DataFrame(index=[i for i in range(5)])df.columns = ["res"+str(i) for i in range(5)]Traceback (most recent call last):  File "<stdin>", line 1, in <module>  File "C:\Python35\lib\site-packages\pandas\core\generic.py", line 5080, in __setattr__    return object.__setattr__(self, name, value)  File "pandas\_libs\properties.pyx", line 69, in pandas._libs.properties.AxisProperty.__set__  File "C:\Python35\lib\site-packages\pandas\core\generic.py", line 638, in _set_axis    self._data.set_axis(axis, labels)  File "C:\Python35\lib\site-packages\pandas\core\internals\managers.py", line 155, in set_axis    'values have {new} elements'.format(old=old_len, new=new_len))ValueError: Length mismatch: Expected axis has 0 elements, new values have 5 elements不明白這是什么原因。如果我有索引,那么為什么沒有添加該列?
查看完整描述

2 回答

?
POPMUISE

TA貢獻(xiàn)1765條經(jīng)驗(yàn) 獲得超5個(gè)贊

我認(rèn)為問題首先是沒有創(chuàng)建列DataFrame,所以如果要分配列名它失敗了。


解決方案是將參數(shù)添加columns到 DataFrame 構(gòu)造函數(shù),索引應(yīng)簡(jiǎn)化 - 僅分配range:


df = pd.DataFrame(index= range(5), columns=["res"+str(i) for i in range(5)])

print (df)

  res0 res1 res2 res3 res4

0  NaN  NaN  NaN  NaN  NaN

1  NaN  NaN  NaN  NaN  NaN

2  NaN  NaN  NaN  NaN  NaN

3  NaN  NaN  NaN  NaN  NaN

4  NaN  NaN  NaN  NaN  NaN

如果想在只有索引的 DataFrame 之后分配,可以使用DataFrame.reindex:


df = pd.DataFrame(index=range(5))

df = df.reindex(["res"+str(i) for i in range(5)], axis=1)

print (df)

   res0  res1  res2  res3  res4

0   NaN   NaN   NaN   NaN   NaN

1   NaN   NaN   NaN   NaN   NaN

2   NaN   NaN   NaN   NaN   NaN

3   NaN   NaN   NaN   NaN   NaN

4   NaN   NaN   NaN   NaN   NaN


查看完整回答
反對(duì) 回復(fù) 2022-07-26
?
LEATH

TA貢獻(xiàn)1936條經(jīng)驗(yàn) 獲得超7個(gè)贊

import pandas as pd

df = pd.DataFrame(index= range(5))

for i in range(5):

  df['res'+str(i)]= '0'

print (df)


  res0 res1 res2 res3 res4

0    0    0    0    0    0

1    0    0    0    0    0

2    0    0    0    0    0

3    0    0    0    0    0

4    0    0    0    0    0


查看完整回答
反對(duì) 回復(fù) 2022-07-26
  • 2 回答
  • 0 關(guān)注
  • 70 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)