0

Is there a way to convert pandas dataframe to vectors? For example,

df
Out[53]: 
     Col1
3    Place
4    Country

Expected output

df_converted = 'Place','Country'
2
  • Expected output: df_converted = 'Place','Country' does't make sense in either Python or pandas. Do you mean a list ['Place','Country']`, a pandas array, a numpy array, numpy vector...? And what do you ultimately want to do with the list/vector, use it in a computation? Commented Feb 1, 2021 at 7:58
  • Also please tag and title pandas stuff pandas not just python, that will help it get seen and get you get faster answers from pandas users. Commented Feb 1, 2021 at 8:00

2 Answers 2

2

You can use method values on a series. This returns a numpy array.

import pandas as pd

df = pd.DataFrame({
    'Col1': ['Place', 'Country'],
    'Col2': ['This', 'That'],
})

vector = df['Col1'].values

print(vector)
print(type(vector))

Output

['Place' 'Country']
<class 'numpy.ndarray'>
Sign up to request clarification or add additional context in comments.

Comments

1

Pandas dataframes are consisted of Series, the code that you shared above is about converting a Serie into a list. You can simply run following code ;

df_converted = list(df["Col1"])

If you want to convert a dataframe into another format such as a numpy array you can find more info at https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_numpy.html for the to_numpy() method.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.