0

I have encountered some value error when input txt file into python.

the txt file called "htwt.txt", and contain the below data:

Ht Wt

169.6 71.2

166.8 58.2

157.1 56

181.1 64.5

158.4 53

165.6 52.4

166.7 56.8

156.5 49.2

168.1 55.6

165.3 77.8

When I typed the below code, and value errors are occurred.

import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

import os


import statsmodels.api as sm

from statsmodels.formula.api import ols


os.chdir("/Users/James/Desktop/data/")

data1=np.loadtxt("htwt.txt") 

df1=pd.DataFrame(data1)

ValueError: could not convert string to float: 'Ht'

May I know what should the correct code so that it can be converted to the data frame? thanks.

4 Answers 4

1

Pandas read_csv is enough

import pandas as pd
import os
os.chdir("/Users/James/Desktop/data/")

df1 = pd.read_csv("htwt.txt",sep=' ')

Output:

>>> df1
      Ht    Wt
0  169.6  71.2
1  166.8  58.2
2  157.1  56.0
3  181.1  64.5
4  158.4  53.0
5  165.6  52.4
6  166.7  56.8
7  156.5  49.2
8  168.1  55.6
9  165.3  77.8

Checking the types:

>>> df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 10 entries, 0 to 9
Data columns (total 2 columns):
 #   Column  Non-Null Count  Dtype  
---  ------  --------------  -----  
 0   Ht      10 non-null     float64
 1   Wt      10 non-null     float64
dtypes: float64(2)
memory usage: 288.0 bytes
Sign up to request clarification or add additional context in comments.

Comments

1

Like mentioned above pandas read_csv works, but if you insist on using np.loadtxt you can skip the first row which can't be converted to a float. You can do:

data1 = np.loadtxt("htwt.txt", skiprows=1)

1 Comment

Please see OP's comment on Alexander's answer.
0

The first row in your text file has alphanumeric characters: "Ht Wt". These characters cannot be converted to a floating point number. Remove the first row and you should be fine.

1 Comment

Hi Alexander, any ways that do not need to remove the first row but change them into column name when import them? thanks a lot!
0
#for skipping of the first line
file1 = open("hwwt.txt")

lines = file1.readlines()[1:]
for line in lines:
    print(line.rstrip())

OUTPUT
#otherwise you can use read_csv which is enough

1 Comment

Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.