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'
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'
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.
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'>