user12435585
user12435585

Reputation:

how to clean string data

I extract values from pandas and for number values I embarrass in bracket float(num) which return only integer.

However when I request a string object like:

a.subject - it returns

20    Math
Name: Class10, dtype: object

howe to get only Math without the rest in more efficient way?

Upvotes: 0

Views: 326

Answers (2)

jezrael
jezrael

Reputation: 863301

Problem is one element Series, so instead a.subject select by index and columns labels by DataFrame.loc:

a.loc['Class10','subject']

Or DataFrame.at:

a.at['Class10','subject']

Or if you want first value of subject:

a.loc[a.index[0], 'subject']

Or:

a.iloc[0, a.columns.get_loc('subject')]

Also is possible select by column name and then by position:

a.subject.iat[0]
a.subject.iloc[0]
a.subject.to_numpy()[0]

If possible a.subject is sometimes empty, selecting failed. Then use:

next(iter(a.subject), 'no match')

Upvotes: 2

cwalvoort
cwalvoort

Reputation: 1967

Use at to return only the value:

import pandas
import numpy

a = pandas.DataFrame(numpy.random.randn(5,1),columns=['subject'])

print(a)
print(a.at[1, 'subject'])

result:

    subject
0 -0.010719
1  0.908645
2 -0.552890
3 -0.756518
4 -0.831926

0.908645173602986

Upvotes: 0

Related Questions