Python2 / numpy> Replace only a specific column in a file with column data from another file> numpy.c_

Operating environment


GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 14.04 LTS desktop amd64
TensorFlow v0.11
cuDNN v5.1 for Linux
CUDA v8.0
Python 2.7.6
IPython 5.1.0 -- An enhanced Interactive Python.
gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
GNU bash, version 4.3.8(1)-release (x86_64-pc-linux-gnu)

Run in Python2 environment.

file organization

input.tab

Tab-delimited files.

x y z E v1 v2
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6

result.csv

3, 1, 4, 1, 5
3, 1, 4, 1, 5
3, 1, 4, 1, 5

It is assumed that the values of x, y, z, v1, v2 are stored.

output.tab

I want to create the following file.

x y z E v1 v2
3.000 1.000 4.000 4.000 1.000 5.000 
3.000 1.000 4.000 4.000 1.000 5.000 
3.000 1.000 4.000 4.000 1.000 5.000 

--I want to replace values other than E with the values in result.csv --Any number of decimal places

code v0.1

* Used in Python3 cannot be used in Python2. I tried various things and ended up with the following solid writing.

merge_170503.py


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np

tabdat = np.genfromtxt('input.tab', delimiter=' ')
csvdat = np.genfromtxt('result.csv', delimiter=',')

for aline in zip(tabdat[1:], csvdat):  # 1:skip title line
	for idx in range(0, 3):
		print("%.3f" % aline[1][idx], end=' ')
	print("%.3f" % aline[0][3], end=' ')
	for idx in range(3, 5):
		print("%.3f" % aline[1][idx], end=' ')
	print()

$ python merge_170503.py 
3.000 1.000 4.000 4.000 1.000 5.000 
3.000 1.000 4.000 4.000 1.000 5.000 
3.000 1.000 4.000 4.000 1.000 5.000 

When I try to run it in Python3, I get the following problem. We have to think about improving the environment.

No module named numpy

Matters taught

@ shiracamus's Comment introduced a clean code using numpy.c_.

Thank you for the information. https://docs.scipy.org/doc/numpy/reference/generated/numpy.c_.html

A search on numpy.c_ also found an article on Qiita. Concatenate matrices with Numpy NumPy array operation (2)

Recommended Posts

Python2 / numpy> Replace only a specific column in a file with column data from another file> numpy.c_
Read a file in Python with a relative path from the program
Read table data in PDF file with Python
Read line by line from a file with Python
Extract data from a web page with Python
Receive dictionary data from a Python program in AppleScript
Run a Python file with relative import in PyCharm
[Python] Leave only the elements that start with a specific character string in the array
A memo that reads data from dashDB with Python & Spark
[Python] Extracts data frames that do not match a specific column with other data frames of Pandas
Count specific strings in a file
Get data from Quandl in Python
Create a binary file in Python
Get data from MySQL on a VPS with Python 3 and SQLAlchemy
[Python] Read a csv file with a large data size using a generator
Replace the directory name and the file name in the directory together with a Linux command.
A memo organized by renaming the file names in the folder with python
Extract lines that match the conditions from a text file with python
Make a decision tree from 0 with Python and understand it (4. Data structure)
Python / numpy> Read the data file with the item name line> Use genfromtxt ()
How to sort by specifying a column in the Python Numpy array.
Get additional data in LDAP with python
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
Receive textual data from mysql with python
Creating a simple PowerPoint file with Python
[Note] Get data from PostgreSQL with Python
Exclusive control with lock file in Python
[Note] Read a file from another directory
Add a Python data source with Redash
Try working with binary data in Python
Replace non-ASCII with regular expressions in Python
Create a deb file from a python package
[GPS] Create a kml file in Python
Generate a class from a string in Python
I made a configuration file with Python
Word count that counts only words that start with a capital letter in python
Python --Read data from a numeric data file and find the multiple regression line.
How to drop Google Docs in one folder in a .txt file with python
Replace the named entity in the read text file with a label (using GiNZA)
Extract elements (using a list of indexes) in a NumPy style from a Python list / tuple
Get time series data from k-db.com in Python
How to read a CSV file with Python 2/3
Create a GIF file using Pillow in Python
[Python] Get the files in a folder with Python
I tried to make a function to retrieve data from database column by column using sql with sqlite3 of python [sqlite3, sql, pandas]
Read a file containing garbled lines in Python
Delete data in a pattern with Redis Cluster
[Python] Swapping rows and columns in Numpy data
From file to graph drawing in Python. Elementary elementary
How to create a JSON file in Python
Create a virtual environment with conda in Python
A story stuck with handling Python binary data
Clone with a specific branch / tag in GitPython
A memo to generate a dynamic variable of class from dictionary data (dict) that has only standard type data in Python3
Data analysis in Python: A note about line_profiler
Work in a virtual environment with Python virtualenv.
Draw a graph with matplotlib from a csv file
Create a decision tree from 0 with Python (1. Overview)
Create a new page in confluence with Python
Call a Python script from Embedded Python in C ++ / C ++
Create a datetime object from a string in Python (Python 3.3)