python - pandas to_csv arguments float_format and decimal not working for index column -
background
i doing simulations resp. system analysis variing parameters (in case rpm
only) , append every last line of results dataframe results_df
summarizing dataframe df
containing giving baviour of system in depencence of varied rpm
.
in order appropriate index plotting , data analysis converted varied values (here rpm
) list pandas series ser
, concat series summarizing dataframe df
containing results interested in.
since results of each calculation interested in last line of each calculation extracting data results dataframe results_df
using .tail(1)
.
what have done far shown in following snippet:
rpm = [0.25, 0.3, 0.5, 0.75, 1.0, 1.5, 2.0] ser = pd.series(rpm, name='rpm') df = pd.dataframe() df_list = list() i, val in enumerate(rpm): results_df = get_some_data_from_somwhere() df_list.append(results_df.tail(1)) df = df.append(df_list, ignore_index=true) df = pd.concat([df, ser], axis=1) df.set_index('rpm', inplace=true) open('foo.csv', 'w') f: data.to_csv(f, index=true, header=true, decimal=',', sep=' ', float_format='%.3f')
problem
this csv-file has follwing format:
rpm cooling_inner heating_inner cooling_outlet heating_outlet 0.25 303,317 323,372 302,384 324,332
however, expected having 3 decimal digits , comma decimal sign on index column, shown here:
rpm cooling_inner heating_inner cooling_outlet heating_outlet 0,250 303,317 323,372 302,384 324,332
so seems index
, decimal
sign options not applied index column when exporting dataframes csv-files using .to_csv
command.
how achieve behaviour since index
option set true
, values (with exception index column) have right format , decimal sign?
do have handle index column somehow seperate?
i rewrite 2 bottom lines:
with open('foo.csv', 'w') f: data.to_csv(f, index=true, header=true, decimal=',', sep=' ', float_format='%.3f')
into
data.reset_index().to_csv('foo.csv', index=false, header=true, decimal=',', sep=' ', float_format='%.3f')
this bit of workaround, have noticed, keyword arguments decimal=
, float_format=
work on data columns, not on index.
what instead put index dataframe reset_index
, tell to_csv(index=false
not save index file (since in data).
also, opening file stream (with open('foo.csv', 'w') f:
) better left pandas, when give string 'foo.csv'
first argument.
Comments
Post a Comment