semchena31
semchena31

Reputation: 51

Pandas DataFrame with unequal columns to be filled with NA s

I want to create a data frame with 2 columns: deneme, deneme2 in Python.

deneme = [1, 2, 3, 4]
deneme2 = ['a', 'b', 'c']

They should look like this: enter image description here

Meaning those two lists should be on the same length and if they are not and if there is an element at the end, it should mean that the third row must be NA.

How may I do such a thing?

Putting them into dataframe directly such as:

pd.DataFrame(deneme, deneme2) 

does not work. Thank you!

Upvotes: 0

Views: 556

Answers (2)

BENY
BENY

Reputation: 323226

Just 3 steps

out = pd.DataFrame({'deneme':deneme})
out.loc[:len(deneme2)-2,'deneme2'] = deneme2[:-1]
out.loc[len(deneme2),'deneme2'] = deneme2[-1]
out
Out[456]: 
   deneme deneme2
0       1       a
1       2       b
2       3     NaN
3       4       c

Upvotes: 0

Giorgos Myrianthous
Giorgos Myrianthous

Reputation: 39810

If the empty values need to be inserted into specific records, then you need to specify None into the corresponding positions:

import pandas as pd

deneme = [1, 2, 3, 4]
deneme2 = ['a', 'b', None, 'c']

df = pd.DataFrame({'deneme': deneme, 'deneme2': deneme2})

and the output will be

   deneme deneme2
0       1       a
1       2       b
2       3    None
3       4       c

If you want to add empty values to the very end, you can use itertools.zip_longest:

import pandas as pd
import itertools

deneme = [1, 2, 3, 4]
deneme2 = ['a', 'b','c']

df = pd.DataFrame((e for e in itertools.zip_longest(deneme, deneme2)), columns=['deneme', 'deneme2'])

and the output will be

   deneme deneme2
0       1       a
1       2       b
2       3       c
3       4    None

Upvotes: 2

Related Questions