Reputation: 5940
Description: I have a set of parameters (par1
, par2
, par3
, par4
) and a dataframe df
. In this example the parameters and the number of columns of the dataframe are respectively are 4 and 3 but they could both be a generic number.
import pandas as pd
import numpy as np
# list of parameters
par1 = 1.05
par2 = 20
par3 = 50000
par4 = 12315468
# Dataframe
dic = {'A' : ['PINCO','PALLO','TOLLO','FINGO','VOLVA'],
'B' : [ 4 , 5 , np.nan, 1 , 0],
'C' : [ 1 , 4 , 8 , 7 , 6]}
df = pd.DataFrame(dic)
My goal is to save this data in the same .csv file but I don't know how to do it since the number of parameters doesn't match the number of columns of df
.
My output file must should follow this rule:
Therefore it looks like this:
Question: Could you provide a smart and efficient way to obtain the output file with the desired shape?
Upvotes: 2
Views: 3381
Reputation: 863166
You can first create list of parameters pars
, then overwrite columns by pars
with same length
and last use reindex
, but values has to be unique
:
# list of parameters
par1 = 1.05
par2 = 20
par3 = 50000
par4 = 12315468
pars = [par1,par2,par3,par4]
# Dataframe
dic = {'A' : ['PINCO','PALLO','TOLLO','FINGO','VOLVA'],
'B' : [ 4 , 5 , np.nan, 1 , 0],
'C' : [ 1 , 4 , 8 , 7 , 6]}
df = pd.DataFrame(dic)
df.columns = pars[:len(pars) - 1]
print (df)
1.05 20.00 50000.00
0 PINCO 4.0 1
1 PALLO 5.0 4
2 TOLLO NaN 8
3 FINGO 1.0 7
4 VOLVA 0.0 6
df = df.reindex(columns=pars)
print (df)
1.05 20.00 50000.00 12315468.00
0 PINCO 4.0 1 NaN
1 PALLO 5.0 4 NaN
2 TOLLO NaN 8 NaN
3 FINGO 1.0 7 NaN
4 VOLVA 0.0 6 NaN
Another possible solution is use concat
of DataFrame
created from list pars
:
pars = [par1,par2,par3,par4]
# Dataframe
dic = {'A' : ['PINCO','PALLO','TOLLO','FINGO','VOLVA'],
'B' : [ 4 , 5 , np.nan, 1 , 0],
'C' : [ 1 , 4 , 8 , 7 , 6]}
df = pd.DataFrame(dic)
print (df)
df.columns = range(len(df.columns))
s = pd.DataFrame([pars])
print (s)
0 1 2 3
0 1.05 20 50000 12315468
df1 = pd.concat([s, df], ignore_index=True)
print (df1)
0 1 2 3
0 1.05 20.0 50000 12315468.0
1 PINCO 4.0 1 NaN
2 PALLO 5.0 4 NaN
3 TOLLO NaN 8 NaN
4 FINGO 1.0 7 NaN
5 VOLVA 0.0 6 NaN
EDIT Also is possible use mode a
for append in read_csv
:
filename = 'filename.csv'
pars = [par1,par2,par3,par4]
pd.DataFrame([pars]).to_csv(filename, index=False, header=False)
df.to_csv(filename, index=False, header=False, mode='a')
Upvotes: 2
Reputation: 2335
First create list of parameters par1
, par2
, par3
, etc,.
l = [par1, par2, par3, par4]
Then save the list to csv
filename = 'abc.csv'
pd.DataFrame(l).T.to_csv(filename, index=False, header=False)
Use python's append mode to append the data frame to csv.
with open(filename, 'a') as f: # Use append mode.
df.to_csv(f, index=False, header=False)
Upvotes: 4