0

I am extracting 2 columns from 2 different CSV with panda and I would like to print the 2 columns extracted.

I have no problems printing out each column separately, but when I want to print the 2 columns inline, it returns NaN values instead of float.

To be clearer, here's the code:

import pandas as pd

col_list = ["ID",]
df = pd.read_csv("waterproofposters.csv", usecols=col_list)

col_list = ["Regular price",]
dg = pd.read_csv("tempwaterproofposters.csv", usecols=col_list,)

dh = pd.DataFrame((df)|(dg))

print (df)
Output: 
          ID
     0    23770.0
     1    23771.0
     2    23772.0
     3    23773.0
     4    23774.0

print (dg)
Output: 
        Regular price
     0          25.79
     1          40.19
     2          55.19
     3          69.59

print (dh)
Output:
          ID  Regular price
     0   NaN            NaN
     1   NaN            NaN
     2   NaN            NaN
     3   NaN            NaN
     4   NaN            NaN

Wanted output:

     ID          Regular price
0    23770.0      25.79
1    23771.0      40.19
2    23772.0      55.19
3    23773.0      69.59
4    23774.0      NaN or null
Barbaros Özhan
  • 47,993
  • 9
  • 26
  • 51
Aissa
  • 27
  • 6
  • Are you trying to merge two dataframes, or two series into a dataframe ? Would this answer help : https://stackoverflow.com/questions/18062135/combining-two-series-into-a-dataframe-in-pandas ? – GuitarExtended Jun 20 '21 at 16:08
  • @GuitarExtended The quoted link is for combining 2 series. Here is 2 dataframes. – SeaBean Jun 20 '21 at 18:01

1 Answers1

2

You can use .join() to join the 2 dataframes vertically, instead of dh = pd.DataFrame((df)|(dg))

dh = df.join(dg)

Result:

print(dh)

        ID  Regular price
0  23770.0          25.79
1  23771.0          40.19
2  23772.0          55.19
3  23773.0          69.59
4  23774.0            NaN

SeaBean
  • 21,029
  • 3
  • 10
  • 23
  • dh = df.join(dg) worked perfectly. Thank you very much. You've made my day, I would like to upvote your contribution but I can't because I'm a new contributor – Aissa Jun 20 '21 at 13:25
  • @user16261242 Glad it worked for you. You can still accept the answer by clicking ✓ on the left (see [How to accept SO answers](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)) – SeaBean Jun 20 '21 at 13:38
  • 1
    Thanks for the clarification, I have just marked ✓ your answer. – Aissa Jun 20 '21 at 13:58