0
votes

I'm trying to import other dataframe columns from various files into one file.

Let's say df1, df2 and df3 are each in different .py files but the files are saved in the same folder.

ie df1 is saved in df1.py df2 is saved in df2.py df3 is saved in df3.py

I am trying to figure out how to import a column from df2 and df3 into df1 (which has 7 rows) even though all dataframes have different number of rows.

NaN rows should be represented as a - (ie a dash '-')

df1 = pd.DataFrame()

df1['A'] = (1,22,13,41,2,56,79)
df1['B'] = ('B','S','S', 'S', 'S', 'B', 'B')
df1['C'] = (1.02, 1.01, 1.44, 1.05, 1.05, 1.12, 1.22)



df2 = pd.DataFrame()


df2['D'] = (11,65,8,34,56,18,91,34,89,35,3,9,15)
df2['E'] = ('E','R','Y', 'N', 'X', 'T','Y', 'N', 'X', 'T', 'D', 'T', 'D')
df2['F'] = (1.02, 1.01, 1.44, 1.05,1.02, 1.01, 1.44, 1.05, 1.05, 1.12, 1.22, 1.12, 1.22)

df3 = pd.DataFrame()


df3['G'] = (1,22,13)
df3['H'] = ('S','S', 'S')
df3['I'] = (1.05, 1.05, 1.12)

print the below is how I would like df1 to look after importing

df1 = pd.DataFrame()

df1['A'] = (1,22,13,41,2,56,79)
df1['B'] = ('B','S','S', 'S', 'S', 'B', 'B')
df1['C'] = (1.02, 1.01, 1.44, 1.05, 1.05, 1.12, 1.22)
df1['D'] = (11,65,8,34,56,18,91)
df1['E'] = ('E','R','Y', 'N', 'X', 'T','Y')
df1['F'] = (1.02, 1.01, 1.44, 1.05,1.02, 1.01, 1.44)
df1['G'] = (1,22,13, '-', '-', '-', '-')
df1['H'] = ('S','S', 'S', '-', '-', '-', '-')
df1['I'] = (1.05, 1.05, 1.12, '-', '-', '-', '-')

print(df1)
2
So apparently you want to convert your numeric columns to string type, else we cannot convert the NaN to a dash -. Is that correct?Erfan
This matching does not make any sense, can you explain what the logic is here?Erfan
Sorry I'm a noob. I just want to know how to import a particular dataframe column from a seperate python file into the current fileds882

2 Answers

0
votes

Use -

print(pd.concat([df1, df2, df3], axis=1).dropna(subset=['A', 'B', 'C']).fillna('-'))

Output

      A  B     C   D  E     F   G  H     I
0   1.0  B  1.02  11  E  1.02   1  S  1.05
1  22.0  S  1.01  65  R  1.01  22  S  1.05
2  13.0  S  1.44   8  Y  1.44  13  S  1.12
3  41.0  S  1.05  34  N  1.05   -  -     -
4   2.0  S  1.05  56  X  1.02   -  -     -
5  56.0  B  1.12  18  T  1.01   -  -     -
6  79.0  B  1.22  91  Y  1.44   -  -     -
0
votes

You can try:

import pandas as pd
import df1, df2, df3 # import files df1.py, df2.py and df3.py
print(pd.concat([df1.df1, df2.df2.iloc[:df1.df1.shape[0]], df3.df3], axis=1, sort=False).fillna('-'))

Result:

    A  B     C   D  E     F   G  H     I
0   1  B  1.02  11  E  1.02   1  S  1.05
1  22  S  1.01  65  R  1.01  22  S  1.05
2  13  S  1.44   8  Y  1.44  13  S  1.12
3  41  S  1.05  34  N  1.05   -  -     -
4   2  S  1.05  56  X  1.02   -  -     -
5  56  B  1.12  18  T  1.01   -  -     -
6  79  B  1.22  91  Y  1.44   -  -     -