简体   繁体   中英

Make dataframe having different column length with the given lists of different length

I have two lists of different length and i want to form a DataFrame having these lists as columns of a DataFrame. Lets say my lists are L1 =["a","b"] L2 = ["g","h","o","y"] L3 = ["k","u","e"]

i want a dataframe in which looks like

 L1 L2 L3
 a  g  k
 b  h  u
    o  e
    y   

The question is just not limited for only three columns.It is in the range of hundreds

Following results, it might be looking for, the idea is to convert the list into Data frame, you could also create a function that take as input, list arguments and not type every list into Data-Frame by hand.

Example 1:

import pandas as pd 
import numpy as np

## your list
L1 =["a","b"] 
L2 = ["g","h","o","y"] 
L3 = ["k","u","e"]

## Convert List into DataFrame 
L1 = pd.DataFrame(L1)
L2 = pd.DataFrame(L2)
L3 = pd.DataFrame(L3)

## Concanate the created DataFrames and Fill 'NaN' with or empty spaces 
db =pd.concat([L1,L2,L3], ignore_index=True, axis=1).replace(np.nan, '')

## Results
print(db)

Example 2 (Function passing list arguments):

import pandas as pd 
import numpy as np

## your list
L1 =["a","b"] 
L2 = ["g","h","o","y"] 
L3 = ["k","u","e"]

## function passing arguments
def wrapper(*args):
    return pd.concat([pd.DataFrame(i) for i in args], ignore_index=True, axis=1).replace(np.nan, '')


## Results
print(wrapper(L1,L2,L3))

You can use zip_longest iterator from itertools . It will pad the shorter sequences with fillvalue which is None by default. You can specify something else like an empty string in the example below.

from itertools import zip_longest
df = pd.DataFrame(zip_longest(L1,L2,L3, fillvalue=''), 
                  columns=['L1','L2','L3'])

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