ds882
ds882

Reputation: 105

import single pandas dataframe column from another python file

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)

Upvotes: 0

Views: 2310

Answers (3)

Mr Jxtr
Mr Jxtr

Reputation: 118

I am also new to this. But I don't think what you are trying to do is possible since if these df are in separate py files, (if I remember correctly) df are stored in memory and once you close the file, memory will not remember it.

I think it's much better to export this into a CSV or something and import what you want into a separate PY file.

Upvotes: 0

René
René

Reputation: 4827

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   -  -     -

Upvotes: 0

Vivek Kalyanarangan
Vivek Kalyanarangan

Reputation: 9081

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   -  -     -

Upvotes: 0

Related Questions