Convert columns using the first 2 elements… here is a solution to the problem.
Convert columns using the first 2 elements
I have this code:
import pandas as pd
data = pd.read_csv('data.csv', header=None)
print((data[[7]].str[:2]))
My data is like this:
1,a,0,11,1,11.09,aa, 0 dollars
1,b,0,11,1,17.38,bb, 1 dollar
3,c,0,11,1,24.68,cdd, 12 dollares
I’m trying to convert my dataset to:
1,a,0,11,1,11.09,aa, 0
1,b,0,11,1,17.38,bb, 1
3,c,0,11,1,24.68,cdd, 12
When I run my code, I get this error :
builtins. AttributeError: 'DataFrame' object has no attribute 'str'
How do I convert my data into the data I want?
Thanks!
Solution
You need to remove a []
for the Series
, here use double []
to create a column of DataFrame
:
data[7] = data[7].str[:2]
print (data)
0 1 2 3 4 5 6 7
0 1 a 0 11 1 11.09 aa 0
1 1 b 0 11 1 17.38 bb 1
2 3 c 0 11 1 24.68 cdd 1
However, if you want to extract numeric values, use extract
and converted to integer
s:
data[7] = data[7].str.extract('(\d+)', expand=False).astype(int)
print (data)
0 1 2 3 4 5 6 7
0 1 a 0 11 1 11.09 aa 0
1 1 b 0 11 1 17.38 bb 1
2 3 c 0 11 1 24.68 cdd 12