[英]Panda Numpy converting data to a column
I have a data result that when I print it looks like我有一个数据结果,当我打印时它看起来像
>>>print(result)
[[0]
[1]
[0]
[0]
[1]
[0]]
I guess that's about the same as [ [0][1][0][0][1][0] ] which seems a bit weird [0,1,0,0,1,0] seems a more logical representation but somehow it's not like that.我想这与 [ [0][1][0][0][1][0] ] 差不多,这看起来有点奇怪 [0,1,0,0,1,0] 似乎更合乎逻辑但不知何故它不是那样的。
Though I would like these values to be added as a single column to a Panda dataframe df虽然我希望将这些值作为单个列添加到 Panda 数据框 df
I tried several ways to join it to my dataframe:我尝试了几种方法将它加入我的数据框:
df = pd.concat(df,result)
df = pd.concat(df,{'result' =result})
df['result'] =pd.aply(result, axis=1)
with no luck.没有运气。 How can I do it?我该怎么做?
如果您希望将该数组放入平面格式的 Pandas 数据框列中,以下是最简单的方法: df["result"] = sum(result, [])
There is multiple ways for flatten your data:有多种方法可以展平数据:
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
As long as the number of data points in this list is the same as the number of rows of the dataframe this should work:只要此列表中的数据点数与数据帧的行数相同,这应该有效:
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.