Home > Mobile >  Pass in a string from txt file into pd.read.csv
Pass in a string from txt file into pd.read.csv

Time:05-12

I have a txt file of strings consisting of 2 rows 1 column in which I want to pass in the first row string into pd.read.csv

import pandas as pd
data = pd.read_csv('datainput.txt',  header=None)
data.columns = ["a"]
print(data.iloc[0])

df = pd.read_excel(data.iloc[0].astype('string'))
outT = data.iloc[1].astype('string')

.astype('string') does not work

Error:

 a  test.xlsx
 Invalid file path or buffer object type: <class 'pandas.core.series.Series'>

Desired Output

Simply pass in a string into pd.read.csv

CodePudding user response:

Why not just read in the file as is?

from pathlib import Path
xl_path, outT = Path("data.txt").read_text().split("\n")
df = pd.read_excel(xl_path)

CodePudding user response:

You can use

df = pd.read_excel(data['a'].iloc[0])

If your DataFrame is natural number indexed, you can also use

df = pd.read_excel(data.loc[0, 'a'])
  • Related