您可以先保存您擁有的數據幀,然後在需要時加載另一個python腳本。你可以使用兩個軟件包:pickle
和shelve
。
要與pickle
做到這一點:
import pickle
with open('dataframe', 'rb') as pfile:
df2 = pickle.load(pfile) # read the dataframe stored in file "dataframe"
print(df2)
輸出:
income user
0 40000 Bob
1 50000 Jane
2 42000 Alice
要做到這一點與shelve
:
import pandas as pd
import pickle
df = pd.DataFrame({'user': ['Bob', 'Jane', 'Alice'],
'income': [40000, 50000, 42000]})
with open('dataframe', 'wb') as pfile:
pickle.dump(df, pfile) # save df in a file named "dataframe"
要在另一個文件中讀取數據框
import pandas as pd
import shelve
df = pd.DataFrame({'user': ['Bob', 'Jane', 'Alice'],
'income': [40000, 50000, 42000]})
with shelve.open('dataframe2') as shelf:
shelf['df'] = df # store the dataframe in file "dataframe"
要讀取另一個文件中的數據框:
import shelve
with shelve.open('dataframe2') as shelf:
print(shelf['df']) # read the dataframe
輸出:
income user
0 40000 Bob
1 50000 Jane
2 42000 Alice
你的問題沒有任何意義。在另一個程序中執行?你在說什麼?蟒蛇,R?只需將數據框保存爲csv並將其加載到其他程序中即可 –