DataFrame to CSV creates empty files

78
February 01, 2019, at 08:50 AM

I have one large dataframe which I am splitting into multiple smaller dataframes based on a particular column and want to save each of these smaller dataframes as individual csv files.

all_parameters is a list of strings that appear in a particular column of the dataframe. column_name is the column in dataframe dfthat has the names I am interested in.

for parameter in all_parameters:
    output_filename=str(parameter)+'_char.txt'
    df_param=df.loc[df[column_name]==parameter]
    df_param.reset_index(inplace=True,drop=True)
    print("\n"+output_filename)
    print(df_param.head())
    df_param.to_csv(output_filename)

Each dataframe df_param prints as expected but when I try to save to a csv, it just creates 0KB files with a name without the '_char.txt'

If I change to a fixed filename as in df_param.to_csv('1.txt') it saves the last file in the loop correctly.

To clarify- The print statement in the code above generates multiple dataframes like this:

[5 rows x 11 columns]
s8srssf8a_VTRIP_LVPORBOD_fall:tripLevel_char.txt
   x  y  SITE     ...           Data             Source Table  Temperature
0  6  1     1     ...       1610.436    1D.001.20190131091926           25
1  6  1     1     ...       1548.332  1D.001.20190131103844U1          130
2  5  1     1     ...       1608.646    1D.001.20190131120359          -40
3  8  1     1     ...       1597.414    1D.001.20190131120359          -40
4  1  1     1     ...       1608.345    1D.001.20190131111528           90
[5 rows x 11 columns]
s8srssf8a_VTRIP_LVPORBOD_rise:tripLevel_char.txt
   x  y  SITE     ...           Data             Source Table  Temperature
0  3  1     1     ...       1574.713  1D.001.20190131103844U1          130
1  1  1     1     ...       1529.827  1D.001.20190131103844U1          130
2  2  1     1     ...       1621.061    1D.001.20190131091926           25
3  1  1     1     ...       1631.120    1D.001.20190131091926           25
4  7  1     1     ...       1625.103    1D.001.20190131120359          -40

What is going on here? Help please!

READ ALSO
CSS java Library

CSS java Library

I hope you doing wellIs there a CSS Java library to recognize whether a CSS String is valid or not? Something like this:

64
Is there a way of optimizing this Java for loop?

Is there a way of optimizing this Java for loop?

I have 2 tables and i merge them together to produce 1 table which i have an indicator column of left and right so i know which file is whichI then run the below code which loops through each of the columns and looks at every cell then compares 2 records...

60
Reading Text From File Into Variables

Reading Text From File Into Variables

I've got a text file formatted like this:

62
NullPointerException not passing in JUnit test

NullPointerException not passing in JUnit test

I'm having trouble getting my NullPointerException test case to pass

89