简体   繁体   English

从两个现有数据帧中创建一个新的 pandas dataframe

[英]Create a new pandas dataframe out of two existing dataframes

I have two existing dataframes df_A and df_B with columns X and Y :我有两个现有的数据df_Adf_B与列XY

df_A

X Y
abc def
ghi jkl

df_B

X Y
mno pqr
stu vwx
zya bcd

I now want to create a new dataframe df_new which contains the columns name and length , where length is length of df_A and df_B .我现在想创建一个新的 dataframe df_new ,其中包含列namelength ,其中lengthdf_Adf_B的长度。 It should look like this one.它应该看起来像这个。

name length
A 2
B 3

How could I do this?我怎么能这样做?

There are several ways to do this, varying in the level of automation and abstraction.有几种方法可以做到这一点,在自动化和抽象级别上有所不同。 Here is a rather manual way:这是一种相当手动的方法:

df_new = pd.DataFrame({
    'name': ['A', 'B'],
    'length': map(len, [df_A, df_B])
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM