Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamically naming edited csv, to include part of the old csv name
    primarykey
    data
    text
    <p>I am using some code to merge two csvs and sort these by two columns. Ouputting a new csv. The input csvs are of the same name just numbered 1 &amp; 2. I am repeating this code for multiple sets of data. I was wondering what the method would be to make the code output a file name containing the first part of the original file names.</p> <p>My current code: </p> <pre><code>import pandas as pd df1 = pd.read_csv("data csv 1\September 2013 1 UUedit1.csv", delimiter = ",") df2 = pd.read_csv("data csv 1\September 2013 2 UUedit2.csv", delimiter = ",") merged = df1.merge(df2, on="Unique Element") delcols = "Element_y", "number_y", "date_y", "title_y", "name_y" for delcol in delcols: del merged[delcol] merged.rename(columns={"name_x": "name", "rdate_x": "date", "title_x": "title", "number_x": "number", "Element_x": "Element"}, inplace = True) merged = merged.sort("Element").reset_index(drop=True) merged = merged.sort("date").reset_index(drop=True) merged.to_csv("MRG.csv", index=False, sep = ",") </code></pre> <p>So in this example both the input files are called <code>September 2013</code>"number""UUedit" I want to make my code output the file name directly as <code>September 2013 MRG.csv</code> how can this be coded? To clarify if the two original files were <code>October 2013</code> then the output would be <code>October 2013 MRG.csv</code> Many thanks GTPE</p> <h2>Edit</h2> <p>Upon running the code supplied by Christian Ternus I received the following print and traceback: </p> <pre><code>Usage: C:/Test.py &lt;month&gt; &lt;year&gt; Traceback (most recent call last): File "C:/Test.py", line 7, in &lt;module&gt; month, year = sys.argv[1:] ValueError: need more than 0 values to unpack </code></pre> <p>I am unsure what the second variable should be set to.<br> Many thanks<br> GTPE</p> <h3>Edit 2</h3> <p>I managed to get the code to work by calling it CMD, however my attempts at calling the script through python didn't seem to work. I tried the folowing:</p> <pre><code>import subprocess p = subprocess.Popen(['python', 'RawDataSheetMergerPandasTest.py September 2013'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = p.communicate() print out </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload