Python 通过从其他数据帧中提取列来创建新的数据帧-ValueError

Python 通过从其他数据帧中提取列来创建新的数据帧-ValueError,python,python-2.7,pandas,dataframe,Python,Python 2.7,Pandas,Dataframe,我必须从不同的数据帧中提取列,并将它们合并到一个新的数据帧中。这就是我正在做的: newdf=pd.DataFrame() newdf['col1']=sorted(df1.columndf1.unique()) newdf['col2']=df2.columndf2.unique(), newdf['col3']=df3.columndf3.unique() newdf 我确信这三列的长度是相同的(我已经检查过了),但是我得到了错误 ValueError: Length of values

我必须从不同的数据帧中提取列,并将它们合并到一个新的数据帧中。这就是我正在做的:

newdf=pd.DataFrame()
newdf['col1']=sorted(df1.columndf1.unique())
newdf['col2']=df2.columndf2.unique(),
newdf['col3']=df3.columndf3.unique()
newdf
我确信这三列的长度是相同的(我已经检查过了),但是我得到了错误

ValueError: Length of values does not match length of index

我曾尝试将它们作为pd.Series传递,但结果是一样的。我使用的是Python 2.7。

似乎存在唯一值的长度不同的问题

一种可能的解决方案是将所有数据合并在一起并应用
唯一

如果唯一数据大小不同,则在列的最后一个值中获取
NaN
s

newdf = pd.concat([df1.columndf1, df2.columndf2, df3.columndf3], axis=1)
          .apply(lambda x: pd.Series(x.unique()))
编辑:

另一种可能的解决办法:

a = sorted(df1.columndf1.unique())
b = list(df2.columndf2.unique())
c = list(df3.columndf3.unique())

newdf=pd.DataFrame({'col1':a, 'col2':b, 'col3':c})

似乎存在唯一值的长度不同的问题

一种可能的解决方案是将所有数据合并在一起并应用
唯一

如果唯一数据大小不同,则在列的最后一个值中获取
NaN
s

newdf = pd.concat([df1.columndf1, df2.columndf2, df3.columndf3], axis=1)
          .apply(lambda x: pd.Series(x.unique()))
编辑:

另一种可能的解决办法:

a = sorted(df1.columndf1.unique())
b = list(df2.columndf2.unique())
c = list(df3.columndf3.unique())

newdf=pd.DataFrame({'col1':a, 'col2':b, 'col3':c})

在您的案例中,所有三列都具有相同的唯一值?不,它们对于每一列都不同,但是
.unique()
对象的长度相同所有三列在您的案例中都具有相同的唯一值?不,它们对于每一列都不同,但是
.unique()
对象的长度是相同的,但是如果我使用
len进行检查的话(排序后的(df1.columndf1.unique())==len(df2.columndf2.unique())
len(df2.columndf2.unique())==len(df3.columndf3.unique())
这两个参数都是真的,所以如果选中
print(len(df1.columndf1.unique())
,那么
print(len(df2.columndf2.unique())
print(len(df3.columndf1.unique())
所有值都是相同的?是的!只要选中OK,那么如果使用我的代码,最后一行中没有
NaN
s?不客气!我想问题是
unique
返回numpy数组,而不是
list
sb,但如果我选中
len(排序(df1.columndf1.unique())==len(df2.columndf2.unique())
len(df2.columndf2.unique())==len(df3.columndf3.unique())
我对这两个.Iteresting都是真的,所以如果选中
print(len(df1.columndf1.unique())
,然后选择
print(len(df2.columndf2.unique())
print(len(df3.columndf3.unique())
所有值都是相同的?是的!只需选中OK,所以如果使用我的代码,最后一行没有
NaN
s?不客气!我想问题是
唯一的
返回numpy数组,而不是
列表
s