0

I have data in my .txt file as below:

029070 ***** 190101010600 270 36 OVC ** 0.0 ** **

I want to extract 190101 from the column 3, I am getting AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandasbelow is my python pandas. Below is my code

import pandas as pd
import numpy as np
import re

data = pd.read_csv('dummy.txt', sep=" ", low_memory=False, header=None)
data.columns = ["a", "b", "c","d","e","f","g","h","i","j"]

print(data.c.str[0:6])
1

1 Answer 1

3

The problem here is that when you read your txt file, in it is casting "c" as an integer and the .str accessor will not work with non-string dtypes, you can fix this problem a couple of ways:

Option 1

Cast the integer as a string in the print statement.

print(data.c.astype(str).str[0:6])

0    190101
Name: c, dtype: object

Option 2

Cast as a string on the into the dataframe with dtype parameter in read_csv

data = pd.read_csv(txtfile, sep=' ', header=None, dtype={2:'str'})
data.columns = list('abcdefghij')
print(data.c.str[0:6]

0    190101
Name: c, dtype: object
Sign up to request clarification or add additional context in comments.

Comments

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.