-1

My dataset consists of three columns, I required to merge the data into one column. For example, if 1, 2, and 3 are the first entries of each column the merged column should be 123. I attempted to solve this problem with concatenate command but it is not useful. Here is my script:

tr = pd.read_csv("YMD.txt", sep='\t',header=None)

Y = tr[0]
M = tr[1]
D = tr[2]
np.concatenate((Y, M, D)) 
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Adnan
  • 67
  • 6
  • 2
    Please add some of the content of `YMD.txt` to your question. – AKX Jun 09 '21 at 05:43
  • Does this answer your question? [How to merge multiple lists into one list in python?](https://stackoverflow.com/questions/11574195/how-to-merge-multiple-lists-into-one-list-in-python) – Alexey Nikonov Jun 09 '21 at 05:48
  • You actually mean *"string concatenate"* not *"merge"*. cc: @AlexeyNikonov. In your example with 1,2,3 do you want the integer 123 or the string '123'? – smci Jun 09 '21 at 06:24
  • integer 1, 2, 3 not string – Adnan Jun 09 '21 at 06:41

1 Answers1

5

You don't need Pandas or Numpy to read a tab-delimited file and merge the first 3 columns into a new list:

ymd = []
with open("YMD.txt") as f:
    for row in f:
        row = row.strip().split("\t")
        ymd.append("".join(row[:3]))
print(ymd)
AKX
  • 152,115
  • 15
  • 115
  • 172