[英]Creating new pandas dataframe by extracting columns from other dataframes - ValueError
I have to extract columns from different pandas dataframes and merge them into a single new dataframe. 我必须从不同的熊猫数据框中提取列,并将它们合并到一个新的数据框中。 This is what I am doing:
这就是我在做什么:
newdf=pd.DataFrame()
newdf['col1']=sorted(df1.columndf1.unique())
newdf['col2']=df2.columndf2.unique(),
newdf['col3']=df3.columndf3.unique()
newdf
I am sure that the three columns have the same length (I have checked) but I get the error 我确定三列的长度相同(我已经检查过),但是我得到了错误
ValueError: Length of values does not match length of index
I have tried to pass them as pd.Series but the result is the same. 我试图将它们作为pd.Series传递,但结果是相同的。 I am on Python 2.7.
我使用的是Python 2.7。
It seems there is problem length of unique values is different. 似乎唯一值的长度存在问题。
One possible solution is concat all data together and apply unique
. 一种可能的解决方案是将所有数据连接在一起并应用
unique
。
If unique data not same sizes, get NaN
s in last values of columns. 如果唯一数据的大小不同,请在列的最后一个值中获取
NaN
。
newdf = pd.concat([df1.columndf1, df2.columndf2, df3.columndf3], axis=1)
.apply(lambda x: pd.Series(x.unique()))
EDIT: 编辑:
Another possible solution: 另一个可能的解决方案:
a = sorted(df1.columndf1.unique())
b = list(df2.columndf2.unique())
c = list(df3.columndf3.unique())
newdf=pd.DataFrame({'col1':a, 'col2':b, 'col3':c})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.