简体   繁体   中英

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_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 . 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])
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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