Reputation:
So I have two textfiles, "pt1.txt" and "pt2.txt"
Contents of "pt1.txt":
Name,ICO,Max,USD_ROI
Bitcoin,0.0,19535.7,N/A
Ethereum,0.0,1389.18,N/A
Ripple,0.0,3.6491,N/A
Bitcoin Cash,0.0,4091.7,N/A
EOS,0.99,21.4637,2068.05%
Cardano,0.0,1.27977,N/A
Stellar,0.0,0.851652,N/A
Contents of "pt2.txt":
BTC_ROI,ETH_ROI
N/A,N/A
N/A,N/A
N/A,N/A
N/A,N/A
N/A,N/A
-260.77%,-130.75%
N/A,N/A
N/A,N/A
I want to combine "pt1.txt" and "pt2.txt" by adding the contents of "pt2.txt" into "pt1.txt"
Here is my code:
import pandas as pd
f = open("pt1.txt","w+")
df = pd.read_csv("pt2.txt")
f.write(df["BTC_ROI"])
f.write(df["ETH_ROI"])
I got an error saying this:
TypeError: write() argument must be str, not Series
How can I change my code so "pt1.txt" will end up looking like this?
Name,ICO,Max,USD_ROI,BTC_ROI,ETH_ROI
Bitcoin,0.0,19535.7,N/A,N/A,N/A
Ethereum,0.0,1389.18,N/A,N/A,N/A
Ripple,0.0,3.6491,N/A,N/A,N/A
Bitcoin Cash,0.0,4091.7,N/A,N/A,N/A
EOS,0.99,21.4637,2068.05%,-260.77%,-130.75%
Cardano,0.0,1.27977,N/A,N/A,N/A
Stellar,0.0,0.851652,N/A,N/A,N/A
Upvotes: 1
Views: 2461
Reputation: 402813
You're trying to add columns to an existing CSV. The simplest way to do that would be to read both frames into memory, concat
, and then dump them.
df1 = pd.read_csv('pt1.txt')
df2 = pd.read_csv('pt2.txt')
(pd.concat([df1, df2], axis=1)
.to_csv('pt2.txt', index=False, na_rep='N/A')
)
This is the easiest thing to do if your data is small enough.
Upvotes: 3