簡體   English   中英

將2D列表分配給2個Dataframe列Pandas

[英]Assigning 2D list to 2 Dataframe columns Pandas

我嘗試了以下並得到錯誤:

>>> import pandas as pd
>>> df = pd.DataFrame([[0,0],[2,2]])
>>> df
   0  1
0  0  0
1  2  2
>>> y = [[0,0],[2,2],[3,3]]
>>> df["s","d"] = y
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python35\lib\site-packages\pandas\core\frame.py", line 3119, in __setitem__
    self._set_item(key, value)
  File "C:\Python35\lib\site-packages\pandas\core\frame.py", line 3194, in _set_item
    value = self._sanitize_column(key, value)
  File "C:\Python35\lib\site-packages\pandas\core\frame.py", line 3391, in _sanitize_column
    value = _sanitize_index(value, self.index, copy=False)
  File "C:\Python35\lib\site-packages\pandas\core\series.py", line 4001, in _sanitize_index
    raise ValueError('Length of values does not match length of ' 'index')
ValueError: Length of values does not match length of index
>>> df[["s","d"]] = y
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python35\lib\site-packages\pandas\core\frame.py", line 3116, in __setitem__
    self._setitem_array(key, value)
  File "C:\Python35\lib\site-packages\pandas\core\frame.py", line 3142, in _setitem_array
    indexer = self.loc._convert_to_indexer(key, axis=1)
  File "C:\Python35\lib\site-packages\pandas\core\indexing.py", line 1327, in _convert_to_indexer
    .format(mask=objarr[mask]))
KeyError: "['s' 'd'] not in index"

讓我知道如何同時使用2D數組創建2列。

使用DataFrame構造函數並分配給嵌套列表:

df = pd.DataFrame([[0,0],[2,2]])

y = [[0,0],[2,2]]

df[["s","d"]] = pd.DataFrame(y)
print (df)
   0  1  s  d
0  0  0  0  0
1  2  2  2  2

另一個解決方案是創建新的DataFramejoin到原始:

df = df.join(pd.DataFrame(y, columns=['s','d'], index=df.index))

如果要添加多個列:

df = pd.DataFrame([[0,0],[2,2]])

y = [[0,0],[2,2],[3,3]]

df[["s","d","e"]] = pd.DataFrame(np.array(y).T)
print (df)
   0  1  s  d  e
0  0  0  0  2  3
1  2  2  0  2  3

z = [[0,0,3],[2,2,3]]
df[["s","d","e"]] = pd.DataFrame(z)
print (df)
   0  1  s  d  e
0  0  0  0  0  3
1  2  2  2  2  3

如果需要將2個新列附加到3行DataFrame:

df = pd.DataFrame([[0,0],[2,2],[4,4]])

y = [[0,0],[2,2],[3,3]]

df[["s","d"]] = pd.DataFrame(y)
print (df)

   0  1  s  d
0  0  0  0  0
1  2  2  2  2
2  4  4  3  3

否則會得到缺失的值:

df = pd.DataFrame([[0,0],[2,2]])

y = [[0,0],[2,2],[3,3]]

df = df.join(pd.DataFrame(y, columns=['s','d']), how='outer')
print (df)
     0    1  s  d
0  0.0  0.0  0  0
1  2.0  2.0  2  2
2  NaN  NaN  3  3

你不能直接這樣做,但這是一個解決方法:

import pandas as pd

df = pd.DataFrame([[0,0],[2,2]])

y = [[0,0],[2,2]]


df["s"], df["d"] = [i[0] for i in y], [i[1] for i in y]

你有一些問題,其中之一是df['s', 'd']索引不正確,你需要df[['s', 'd']] - 但你也無法分配直接列出到那個。

你也不能指定比索引更長的東西,所以你的y = [[0,0],[2,2],[3,3]]太長了。

暫無
暫無

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

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