Import 2 columns from 8 .csv files into pandas df (side by side) and write a new csv

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Import 2 columns from 8 .csv files into pandas df (side by side) and write a new csv
# 1  
Old 05-20-2015
Import 2 columns from 8 .csv files into pandas df (side by side) and write a new csv

I have 8 .csv files with 16 columns and "n" rows with no Header. I want to parse each of these .csv and get column [0, 8] [#where column 0 is the value of x, y, z etc and column 8 is always the value of a#] and put the data into a new.csv. Once this is done, the new.csv should have 16 columns (2 from each input.csv) and "n" rows.

Now, I want to just take the average of Column[1,3,5,7,9,11,13,15] from new.csv and append it to another file or this file. Basically in the new csv, i want to have the average of colum[8] form the input files and each of the column[0] from the input files. So the final output expecting should have a shape of 9 columns and n rows.
sample input files:

    5.42E+05    6.52E+05    2.17E+04    2.73E+04    2.58E+04    2.33E+04    2.81E+04    3.37E+04    1.08E+08    1.10E+08    2.54E+05    3.21E+05    2.99E+05    2.74E+05    3.39E+05    4.07E+05
    4.64E+04    1.15E+06    1.96E+04    2.53E+04    2.39E+04    2.37E+04    1.98E+04    2.85E+04    6.18E+05    2.17E+08    2.30E+05    3.02E+05    2.75E+05    2.77E+05    2.33E+05    3.42E+05
    4.36E+04    1.13E+06    5.72E+04    2.71E+04    2.77E+04    2.37E+04    2.62E+04    7.35E+04    5.78E+05    2.17E+08    9.26E+05    3.25E+05    3.20E+05    2.72E+05    3.20E+05    1.46E+06
    4.32E+04    1.02E+06    1.47E+05    2.63E+04    3.05E+04    2.26E+04    2.89E+04    2.45E+04    5.70E+05    2.15E+08    2.78E+06    3.02E+05    3.58E+05    2.63E+05    3.49E+05    2.87E+05
    4.44E+04    7.83E+05    1.58E+05    2.95E+04    2.71E+05    2.71E+04    3.67E+04    2.85E+04    5.86E+05    1.61E+08    2.89E+06    3.48E+05    5.39E+07    3.14E+05    4.49E+05    3.39E+05
    1.47E+05    1.02E+06    2.09E+04    2.72E+04    2.66E+04    6.18E+04    3.50E+04    3.00E+04    2.72E+06    2.15E+08    2.46E+05    3.18E+05    3.07E+05    9.91E+05    7.18E+05    3.71E+05
    1.81E+05    7.67E+05    1.94E+04    5.05E+04    2.62E+04    4.50E+04    2.92E+04    2.86E+04    3.16E+06    1.61E+08    2.28E+05    4.84E+06    3.10E+05    5.31E+06    3.49E+05    3.58E+05
    4.94E+05    1.34E+05    6.99E+04    8.76E+05    5.51E+04    5.27E+04    3.34E+05    1.30E+05    1.35E+07    3.59E+06    1.66E+06    1.64E+08    1.03E+06    1.12E+06    5.56E+07    3.37E+06
    4.79E+04    1.38E+05    2.66E+04    1.02E+06    2.85E+04    2.88E+04    2.89E+04    3.26E+04    6.12E+05    2.72E+06    3.21E+05    2.15E+08    3.29E+05    3.39E+05    3.40E+05    4.04E+05
    4.51E+04    6.44E+05    3.02E+04    5.24E+05    2.72E+04    1.89E+04    2.42E+04    3.21E+04    5.97E+05    1.10E+08    3.65E+05    1.07E+08    3.17E+05    2.17E+05    2.85E+05    3.80E+05

        4.25E+03    1.83E+03    1.09E+03    1.35E+03    1.18E+03    1.24E+03    1.16E+03    1.28E+03    1.08E+08    1.10E+08    2.51E+05    3.13E+05    2.80E+05    2.64E+05    3.23E+05    3.32E+05
        4.47E+03    2.20E+03    1.16E+03    1.46E+03    1.28E+03    1.21E+03    1.17E+03    1.36E+03    6.01E+05    2.17E+08    2.92E+05    3.59E+05    3.34E+05    2.84E+05    3.14E+05    3.86E+05
        5.12E+03    1.85E+03    1.62E+03    1.59E+03    1.93E+03    1.36E+03    1.36E+03    1.42E+03    7.19E+05    2.16E+08    1.60E+06    7.14E+06    7.10E+05    8.74E+05    8.67E+05    1.37E+06
        4.32E+03    1.53E+03    2.03E+03    1.11E+03    1.18E+03    1.18E+03    1.52E+03    1.18E+03    5.81E+05    2.15E+08    2.70E+06    2.84E+05    3.24E+05    3.12E+05    4.25E+05    3.65E+05
        4.64E+03    1.53E+03    2.07E+03    1.15E+03    1.15E+03    1.25E+03    1.50E+03    1.13E+03    1.17E+06    1.61E+08    2.74E+06    2.98E+05    2.82E+05    5.38E+07    4.16E+05    3.41E+05
        5.03E+03    1.61E+03    1.17E+03    1.15E+03    1.02E+03    1.12E+03    1.40E+03    1.43E+03    2.56E+06    2.16E+08    2.37E+05    2.57E+05    2.43E+05    2.65E+05    4.03E+05    4.43E+05
        5.11E+03    1.37E+03    1.24E+03    1.20E+03    1.21E+03    1.10E+03    1.28E+03    1.34E+03    3.09E+06    1.61E+08    2.84E+05    2.93E+05    2.91E+05    2.34E+05    5.40E+07    3.07E+05
        5.79E+03    2.51E+03    2.15E+03    2.21E+03    3.57E+03    1.67E+03    2.61E+03    2.28E+03    3.08E+06    4.98E+06    3.60E+06    1.63E+08    7.06E+06    1.95E+06    5.74E+07    3.44E+06
        4.49E+03    1.88E+03    1.22E+03    1.47E+03    1.23E+03    1.04E+03    1.42E+03    1.37E+03    6.11E+05    2.67E+06    2.93E+05    2.15E+08    3.31E+05    2.26E+05    4.13E+05    3.53E+05
        4.50E+03    2.22E+03    1.40E+03    1.34E+03    1.26E+03    1.22E+03    1.18E+03    1.35E+03    6.43E+05    1.10E+08    3.31E+05    1.07E+08    3.50E+05    3.29E+05    3.69E+05    4.26E+05


        1.30E+06    4.34E+05    4.66E+04    0.00E+00    0.00E+00    0.00E+00    0.00E+00    0.00E+00    1.62E+08    5.65E+07    6.02E+06    3.24E+05    3.55E+05    2.83E+05    3.41E+05    4.05E+05
        0.00E+00    1.74E+06    0.00E+00    0.00E+00    0.00E+00    0.00E+00    0.00E+00    0.00E+00    5.61E+05    2.17E+08    3.12E+05    3.34E+05    2.83E+05    2.83E+05    3.01E+05    3.45E+05
        0.00E+00    1.74E+06    0.00E+00    0.00E+00    0.00E+00    0.00E+00    0.00E+00    0.00E+00    6.08E+05    2.17E+08    8.92E+05    3.47E+05    3.43E+05    2.22E+05    3.64E+05    2.38E+05
        0.00E+00    1.74E+06    0.00E+00    0.00E+00    0.00E+00    0.00E+00    0.00E+00    0.00E+00    5.61E+05    2.15E+08    2.90E+06    3.35E+05    3.08E+05    5.85E+05    3.60E+05    3.81E+05
        0.00E+00    1.74E+06    0.00E+00    0.00E+00    0.00E+00    0.00E+00    0.00E+00    0.00E+00    5.45E+05    2.15E+08    2.90E+06    3.11E+05    3.06E+05    2.88E+05    3.73E+05    3.10E+05
        0.00E+00    1.30E+06    0.00E+00    0.00E+00    0.00E+00    0.00E+00    0.00E+00    9.22E+04    4.90E+06    1.65E+08    8.92E+05    3.07E+06    1.37E+06    3.40E+06    1.53E+06    1.52E+07
        0.00E+00    1.74E+06    0.00E+00    4.69E+04    0.00E+00    0.00E+00    0.00E+00    0.00E+00    3.09E+06    2.15E+08    3.08E+05    6.15E+06    3.48E+05    3.63E+05    3.85E+05    4.12E+05
        0.00E+00    0.00E+00    0.00E+00    1.31E+06    0.00E+00    0.00E+00    4.36E+05    0.00E+00    3.06E+06    1.35E+06    2.31E+05    1.61E+08    2.89E+05    2.05E+05    5.41E+07    1.77E+06
        0.00E+00    0.00E+00    0.00E+00    1.74E+06    0.00E+00    0.00E+00    0.00E+00    0.00E+00    6.69E+05    2.27E+06    3.02E+05    2.16E+08    3.27E+05    3.08E+05    3.50E+05    3.75E+05
        0.00E+00    8.69E+05    0.00E+00    8.71E+05    0.00E+00    0.00E+00    0.00E+00    0.00E+00    6.68E+05    1.10E+08    3.07E+05    1.08E+08    3.67E+05    2.34E+05    3.71E+05    3.78

    Final expected output (after averaging column 8):
    5.42E+05    4.25E+03    1.30E+06    125650487
    4.64E+04    4.47E+03    0.00E+00    593233.3333
    4.36E+04    5.12E+03    0.00E+00    634780
    4.32E+04    4.32E+03    0.00E+00    570865
    4.44E+04    4.64E+03    0.00E+00    766418
    1.47E+05    5.03E+03    0.00E+00    3393342.667
    1.81E+05    5.11E+03    0.00E+00    3113608.333
    4.94E+05    5.79E+03    0.00E+00    6532673.333
    4.79E+04    4.49E+03    0.00E+00    630900.3333
    4.51E+04    4.50E+03    0.00E+00    636023

Then I was to do the looping for all the 16 columns (taking the following sets as[n,n+8] where n=0 to 7.

Apologize for the lengthy description, but I can't seem to append the columns in python.
Thanks in advance.

Here is a sample code I started with:

    import csv
    import numpy as np
    import sys
    import pandas as pd
    import glob
    damn = ("a", "b", "c","e","f","g","h","i")
    data = []
    for fles in range(len(damn)):
        core0data = np.genfromtxt('./%s_raw_combine.csv'%damn[fles], dtype=float, delimiter=',',usecols=(0,8))
        #core1data = np.genfromtxt('./%s_raw_combine.csv'%damn[fles], dtype=float, delimiter=',',usecols=(1,9))
        #core2data = np.genfromtxt('./%s_raw_combine.csv'%damn[fles], dtype=float, delimiter=',',usecols=(2,10))
        #core3data = np.genfromtxt('./%s_raw_combine.csv'%damn[fles], dtype=float, delimiter=',',usecols=(3,11))
        #core4data = np.genfromtxt('./%s_raw_combine.csv'%damn[fles], dtype=float, delimiter=',',usecols=(4,12))
        #core5data = np.genfromtxt('./%s_raw_combine.csv'%damn[fles], dtype=float, delimiter=',',usecols=(5,13))
        #core6data = np.genfromtxt('./%s_raw_combine.csv'%damn[fles], dtype=float, delimiter=',',usecols=(6,14))
        #core7data = np.genfromtxt('./%s_raw_combine.csv'%damn[fles], dtype=float, delimiter=',',usecols=(7,15))
    np.savetxt("writer.csv", data, delimiter= ",")

But after running this, I get error:

     > python2.7 
    Traceback (most recent call last):
      File "", line 20, in <module>
        np.savetxt("writer.csv", data, delimiter= ",")
      File "~/anaconda/lib/python2.7/site-packages/numpy/lib/", line 1083, in savetxt
        fh.write(asbytes(format % tuple(row) + newline))
    TypeError: float argument required, not numpy.ndarray

# 2  
Old 05-20-2015
How are you doing your averaging? a# tells me nothing. ? + ? + ? = 125650487
# 3  
Old 05-20-2015
the average you see is: avg(a[8],b[8],c[8])
# 4  
Old 05-20-2015
Ahhh. What you were calling column 8 I considered column 9.

I do not know python, and if I did, I probably wouldn't know all 5 modules you were using for this simple problem.

awk 'BEGIN {
                for(N=1; (N<ARGC); N++)
                        if((getline < ARGV[N]) <= 0) exit;
                        A[ARGC] += $9;

                A[ARGC]/=(ARGC-1);              P="";
                for(N=1; N<=ARGC; (N++) && (P="\t")) printf("%s%s", P, A[N]);
}' a b c

5.42E+05        4.25E+03        1.30E+06        126000000
4.64E+04        4.47E+03        0.00E+00        593333
4.36E+04        5.12E+03        0.00E+00        635000
4.32E+04        4.32E+03        0.00E+00        570667
4.44E+04        4.64E+03        0.00E+00        767000
1.47E+05        5.03E+03        0.00E+00        3.39333e+06
1.81E+05        5.11E+03        0.00E+00        3.11333e+06
4.94E+05        5.79E+03        0.00E+00        6.54667e+06
4.79E+04        4.49E+03        0.00E+00        630667
4.51E+04        4.50E+03        0.00E+00        636000


Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merging two text files side by side

Hi everyone, I need to merge two files side by side The files look something like this: HOSTNAME fishtornado-K52F UPTIME 20:17:01 up 2:19, 3 users, load average: 0.00, 0.04, 0.05 DISK USAGE (Size/Used/Avail/Use%) 29G 6.5G 21G 25% RUN QUEUE PID COMMAND USER ... (4 Replies)
Discussion started by: FishTornado
4 Replies

2. UNIX for Dummies Questions & Answers

Paste many files together side by side

Hi, Thank you for your help in advance :D. I have thousands of files like... file01 file02 file03 . . . file6324 I would like to paste every other file side by side so it looks like file01 file02 file03 file04 ...e.t.c file6323 file6324 (3 Replies)
Discussion started by: shaz1990
3 Replies

3. Shell Programming and Scripting

Combining two single column files side-by-side

Hi, I am looking for a sed/awk script to join two large (~300 M) single column files (one is sorted and the other is not sorted) side-by-side. I have a shell script but its taking ages to do the task so looking for an optimized fast solution. The two files look like: File1 (sorted) a1... (1 Reply)
Discussion started by: sajal.bhatia
1 Replies

4. Shell Programming and Scripting

AWK to merge multiple files side by side

I have about 100s of files of type text in a known directory. I want to merge all files side by side. Number of lines in all the files will remain same. For example file1 contains cat dog File 2 contains rat mat Output file should be cat rat dog mat Using awk I was able to... (5 Replies)
Discussion started by: kanthrajgowda
5 Replies

5. Shell Programming and Scripting

printing 3 files side by side based on similar values in rows

Hi I'm trying to compare 3 or more files based on similar values and outputting them into 3 columns. For example: file1 ABC DEF GHI file2 DEF DER file3 ABC DER The output should come out like this file1 file2 file3 ABC ABC (4 Replies)
Discussion started by: zerofire123
4 Replies

6. Shell Programming and Scripting

Paste two file side by side together based on specific pattern match problem

Input file_1: P78811 P40108 O17861 Q6NTW1 P40986 Q6PBK1 P38264 Q6PBK1 Q9CZ49 Q1GZI0 Input file_2: (6 Replies)
Discussion started by: patrick87
6 Replies

7. Web Development

Cannot access Apache web server from Wan side, only Lan side.

I have installed WAMPSERVER 2.0 on my windows vista x64 system but still am having issues with getting the webserver to be seen outside my local network. It is working fine within my local network. Been through several setup tutorials so far, no dice still. For testing purposes I have... (1 Reply)
Discussion started by: davidmanvell
1 Replies

8. Shell Programming and Scripting

How to Merge / combine / join / paste 2 text files side-by-side

I have 2 text files, both have one simple, single column. The 2 files might be the same length, or might not, and if not, it's unknown which one would be longer. For this example, file1 is longer: ---file1 Joe Bob Mary Sally Fred Elmer David ---file2 Tomato House Car... (3 Replies)
Discussion started by: cajunfries
3 Replies

9. Shell Programming and Scripting

Script to place selected columns from a group of files side by side in a new file

Hi Everyone, I need a shell/perl script to bring selected columns from all the files located in a directory and place them in a new file side by side. File1: a b c d 2 3 4 5 f g h i .......... File2: I II III IV w x y z .............. and so on many files are there...... (8 Replies)
Discussion started by: ks_reddy
8 Replies

10. Shell Programming and Scripting

Merge 3 columns side by side

I know this is a stupid question for you guys! half day googling and i got nothing :( i have 3 variables/files, say: $X1 or file1: # there is one whitespace space after each line | 21 | 9 | 28 | 100 | 51 $X2 or file2: # there is one whitespace space... (7 Replies)
Discussion started by: amaulana
7 Replies
Login or Register to Ask a Question

Featured Tech Videos