Nabih Bawazir
Nabih Bawazir

Reputation: 7255

What is the best way to save list to a file and load id on pandas?

How to save and load list like this so I can load in other place

Index(['province_BANTEN', 'province_DKI JAKARTA', 'province_JAWA BARAT',
       ...
       'lending_count_l3', 'lending_cumcount_l2', 'lending_cumcount_l3'],
      dtype='object', length=341)

I try to save to csv df.to_csv("model_column.csv"), but AttributeError: 'Index' object has no attribute 'to_csv'

Is there any better Idea to save that

Upvotes: 0

Views: 136

Answers (2)

Peacepieceonepiece
Peacepieceonepiece

Reputation: 681

You can use pickle module to save your dataframe such as:

df = pd.DataFrame()

# save dataframe as pickle
with open('model_columns.pickle', 'wb') as f:
    pickle.dump(df, f, protocol=pickle.HIGHEST_PROTOCOL)

# load dataframe as pickle
with open('model_columns.pickle', 'rb') as f:
    new_df = pickle.load(f)

pickle can convert a Python(eg. dataframe) object structure to a stream of bytes and store it.

Upvotes: 1

U13-Forward
U13-Forward

Reputation: 71580

Maybe construct a new dataframe:

df = pd.DataFrame(index=df)
df.to_csv("model_column.csv")

Upvotes: 2

Related Questions