簡體   English   中英

Panda Numpy 將數據轉換為列

[英]Panda Numpy converting data to a column

我有一個數據結果,當我打印時它看起來像

  >>>print(result)

   [[0]
    [1]
    [0]
    [0]
    [1]
    [0]]

我想這與 [ [0][1][0][0][1][0] ] 差不多,這看起來有點奇怪 [0,1,0,0,1,0] 似乎更合乎邏輯但不知何故它不是那樣的。

雖然我希望將這些值作為單個列添加到 Panda 數據框 df

我嘗試了幾種方法將它加入我的數據框:

 df = pd.concat(df,result)
 df = pd.concat(df,{'result' =result})
 df['result'] =pd.aply(result, axis=1)

沒有運氣。 我該怎么做?

如果您希望將該數組放入平面格式的 Pandas 數據框列中,以下是最簡單的方法: df["result"] = sum(result, [])

有多種方法可以展平數據:

df = pd.DataFrame(data=np.random.rand(6,2))

result = np.array([0,1,0,0,1,0])[:, None]
print (result)
[[0]
 [1]
 [0]
 [0]
 [1]
 [0]]


df['result'] = result[:,0]
df['result1'] = result.ravel()
#df['result1'] = np.concatenate(result)

print (df)
          0         1  result  result1
0  0.098767  0.933861       0        0
1  0.532177  0.610121       1        1
2  0.288742  0.718452       0        0
3  0.520980  0.367746       0        0
4  0.253658  0.011994       1        1
5  0.662878  0.846113       0        0

只要此列表中的數據點數與數據幀的行數相同,這應該有效:

import pandas as pd

your_data = [[0],[1],[0],[0],[1],[0]]

df = pd.DataFrame() # skip and use your own dataframe with len(df) == len(your_data)
df['result'] = [i[0] for i in your_data]

暫無
暫無

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

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