3

Here is My dataframe and List


X  Y  Z  X1
1  2  3  3
2  7  2  6
3 10  5  4
4  3  7  9
5  3  3  4

list1=[3,5,6]
list2=[4,3,7,4]

I want to add the lists into a data frame, I have tried some code but it gives an error and something is not working

#Expected Output

X  Y  Z  X1
1  2  3  3
2  7  2  6
3 10  5  4
4  3  7  9
5  3  3  4
3        4
5        3
6        7
         4    


#here is my code

list1 = [3,5, 6]
df_length = len(df1) 
df1.loc[df_length] = list1

Please help me to solve this problem. Thanks in advance.

Coder
  • 1,129
  • 10
  • 24

2 Answers2

3

Use series.append() to create the new series (X & X1), and create the output df using pd.concat():

s1 = df.X.append(pd.Series(list1)).reset_index(drop=True)
s2 = df.X1.append(pd.Series(list2)).reset_index(drop=True)

df = pd.concat([s1, df.Y, df.Z, s2], axis=1).rename(columns={0: 'X', 1: 'X1'})

df
    X   Y       Z   X1
0   1.0 2.0     3.0 3
1   2.0 7.0     2.0 6
2   3.0 10.0    5.0 4
3   4.0 3.0     7.0 9
4   5.0 3.0     3.0 4
5   3.0 NaN     NaN 4
6   5.0 NaN     NaN 3
7   6.0 NaN     NaN 7
8   NaN NaN     NaN 4
CHRD
  • 1,917
  • 1
  • 15
  • 38
  • index starts with 0 with this code!, can you please solve that . – Coder May 22 '20 at 12:21
  • 2
    This gives the expected result! – CHRD May 22 '20 at 12:24
  • I'm not sure what you mean regarding the index. What should it start at if not 0? – CHRD May 22 '20 at 12:33
  • 1
    Great! Feel free to accept if it solved your problem. Cheers! – CHRD May 22 '20 at 12:35
  • 1
    sure will do that – Coder May 22 '20 at 12:38
  • 2
    @CHRD Nice answer +1 – Ch3steR May 22 '20 at 12:47
  • @CHRD df1 = pd.DataFrame([[1,2,2], [1,3, 4], [1,3, 4], [1,3, 4]], columns = ["X", "Y","X1"]) print(df1.head()) list1=[45,45,45] list2=[65,65,65,65,65] s1 = df1.X.append(pd.Series(list1)).reset_index(drop=True) s2 = df1.X1.append(pd.Series(list2)).reset_index(drop=True) df1 = pd.concat([s1, df1.Y, s2], axis=1) df1.head() is there any problem in this code? it adds only one value in dataframe of both list – Coder May 22 '20 at 13:05
  • Not sure I understand. This does exactly what you were asking for, except that you forgot to rename the columns. – CHRD May 22 '20 at 13:09
  • plz run this in your system! your output was expected ! but as i run in my system! it doesn't add all elements of list – Coder May 22 '20 at 13:14
  • I did, and it returns a dataframe with 9 rows as expected – CHRD May 22 '20 at 13:15
  • Happy to help! :) – CHRD May 22 '20 at 13:20
  • After you ask a question here, if you get an acceptable answer, you should “accept” the answer by clicking the check mark next to it. This scores points for you and for the person who answered your question. You can find out more about accepting answers here: [How do I accept an answer?](http://meta.math.stackexchange.com/questions/3286/), [Why should we accept answers?](http://meta.math.stackexchange.com/questions/3399/), [What should I do if someone answers my question?](https://math.stackexchange.com/help/someone-answers). – MarianD May 23 '20 at 09:39
3
'''
X  Y  Z  X1
1  2  3  3
2  7  2  6
3 10  5  4
4  3  7  9
5  3  3  4
'''

list1=[3,5,6]
list2=[4,3,7,4]
ls_empty=[]

import pandas as pd
import numpy as np

df = pd.read_clipboard()


df1 = pd.DataFrame([list1, ls_empty, ls_empty, list2])
df1 = df1.T
df1.columns = df.columns

df2 = pd.concat([df, df1]).replace(np.nan, '', regex=True).reset_index(drop=True).astype({'X1': int})



print(df2)

Output:

   X   Y  Z  X1
0  1   2  3   3
1  2   7  2   6
2  3  10  5   4
3  4   3  7   9
4  5   3  3   4
5  3          4
6  5          3
7  6          7
8             4
Anshul
  • 1,413
  • 2
  • 6
  • 14