0

I have a pandas dataframe with elements that are strings but are expressed as 'objects':

enter image description here

I want to access the string itself. This seems extremely simple but I can't seem to turn it into a string:

enter image description here

I also tried vocab.iloc[0].astype(str) and .astype('|S') as suggested in another post but this still printed something similar to the first picture.

Edit: I just realized that the dataframe titles are the vocab words, and the entries are NaN.... The full dataframe looks something like enter image description here. However I selected the answer that fits the assumption that dataframe entries are strings.

8
  • 1
    Can you post input data and code as text Commented Mar 27, 2021 at 20:16
  • I'm not familiar enough with python to know how to post a simple dataframe of strings, but it's a single-columned dataframe with string entries. Commented Mar 27, 2021 at 20:17
  • Can your print your Dataframe and post a sample, use print(mydataframe.head()) Commented Mar 27, 2021 at 20:20
  • Try converting the entire dataframe to string: vocab.astype(str), then run vocab.iloc[0] Commented Mar 27, 2021 at 20:20
  • @Abhi_J added to the end of the question. Commented Mar 27, 2021 at 20:21

2 Answers 2

2
  1. iloc[n] is used to index a row of a Dataframe not a single value, refer this page
  2. From the image there are NAN values in the data, you can replace them using .fillna()
  3. to get data type of an individual value in datafame try df.iloc[0][0].__class__
  4. to get a single value eg first element use df.iloc[0][0]
  5. to get all values(strings) as a list, use df[<column name>].values or in this case just df.values and then .flatten() to convert it from 2D list to a 1D list

sample:

import pandas as pd #TODO
d1 = ['i', '11', '40', '42', '60' ,'50']
df = pd.DataFrame(d1)
df = df.fillna('')

print(df.iloc[0][0].__class__)

print(df.iloc[0][0])

print(df.values.flatten())

Output:

<class 'str'>

i

['i' '11' '40' '42' '60' '50']
Sign up to request clarification or add additional context in comments.

Comments

1

I tried to create a sample snippet comparable to your use case and extracted the desired element from the dataframe. See the comments in my snippet for further information and explanations:

import io

import pandas as pd


# define some lorem ipsum sample data to work with in this snippet
data_string = """
words
Lorem
ipsum
dolor
sit
amet
consetetur
sadipscing
elitr
sed
diam
"""

# convert string to file-like StringIO and load data into a dataframe
data = io.StringIO(data_string)
df = pd.read_csv(data)

# print the dataframe we will be working with
print(df)

# printing `.info()` will show us that the dataframe consists of objects (same as in your example)
print(df.info())

# let's have a look at the element at integer based index 0 (using `.iloc[0]`)
# this returns a named Series (named `words` here) with a single element
print(df.iloc[0])

# access the series value(s) by calling `.values`
print(df.iloc[0].values)

# as this is a single element Series, we could extract the element
element, = df.iloc[0].values
print(element)

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.