Question: Hi chegg, I am trying to convert a Numpy array to a DataFramewithout using the pd.DataFrame(). My code produces a data frameusing the Series function
Hi chegg, I am trying to convert a Numpy array to a DataFramewithout using the pd.DataFrame(). My code produces a data frameusing the Series function followed by concatenation and the datathat I produce is an array of 1000 by 1 when I require an array of1000 by 9. I can not seem to split the data into the 9 columnswhich is required. Can you help me split the array into 9 columnswithout using pd.DataFrame()? The code I am usingis below, a sample output of the first 5 lines is also shown belowand a sample of what my question requires. Thank you.
My python code below:
def convert_to_df(data):#data is numpy array
#Convert numpy array to pandas dataframe
#column_names={'created_at','tweet_ID','valence_intensity','anger_intensity','fear_intensity','sadness_intensity','joy_intensity','sentiment_category','emotion_category'}
my_array = []
i=0
while i < 1000: #1000 lines of data
my_array.append((data[i]))
i+=1
my_series=pd.Series(my_array)
my_df= pd.concat([my_series],axis=1)
my_df.columns =['test']
my_df['test'].str.split(pat=',', expand=True)#want to split from1 column into 9
print(my_df.shape)
return my_df
My results the python code and note the array is 1000 by1 although only 5 rows are printed:
test
0 [2020-02-29 13:32:59, 1233746991752122368, 0.6...
1 [2020-02-27 00:20:58, 1232822899448385539, 0.4...
2 [2020-02-10 18:54:50, 1226942618195591169, 0.5...
3 [2020-02-29 05:23:06, 1233623708029333505, 0.5...
4 [2020-02-26 03:20:55, 1232505798661722112, 0.4...
(1000, 1)
The required results which has an array of 1000 by 9although only 5 rows are printed:
created_at ... emotion_category0 2020-02-29 13:32:59 ... joy1 2020-02-27 00:20:58 ... fear2 2020-02-10 18:54:50 ... joy3 2020-02-29 05:23:06 ... no specific emotion4 2020-02-26 03:20:55 ... fear[5 rows x 9 columns]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
