How to convert ndarray to pandas DataFrame [duplicate] - pandas

This question already has answers here:
Convert two numpy array to dataframe
(3 answers)
Closed 3 years ago.
I have ndarray data with the shape of (231,31). now I want to convert this ndarray to pandas DataFrame with 31 columns. I am using this code:
for i in range (1,32):
dataset = pd.DataFrame({'Column{}'.format(i):data[:,i-1]})
but this code just creates the last column, it means with 231 indexes and just 1 column, but I need 31 columns. is there any way to fix this problem and why it happens?

Every time you are creating a new dataframe, that is why only the last column remains.
You need to create the dataframe with pd.DataFrame(data).

Related

How can I get an expanded version of a dataframe which has lists as values in it? [duplicate]

This question already has answers here:
How to unnest (explode) a column in a pandas DataFrame, into multiple rows
(16 answers)
Closed 6 months ago.
How can I get an expanded version of a dataframe which has lists as values in it?
Here's a sample of the dataframe I have:
raw = pd.DataFrame().assign(Therapuetic_Area = ['Oncology'],
LocationState = [['Ohio','Illinois','Oregon','New York']])
Now, I need it to look like this edited DataFrame:
edited = pd.DataFrame().assign(Therapuetic_Area = ['Oncology','Oncology','Oncology','Oncology'],LocationState = ['Ohio','Illinois','Oregon','New York'])
Is there a Pandas method I can use for this? How could I get the edited dataframe without having to manually input the values? I can't possibly manually input it because my data is enormously large. Any help would be appreciated!
you can use explode to create rows from the list values
raw.explode('LocationState')
Therapuetic_Area LocationState
0 Oncology Ohio
0 Oncology Illinois
0 Oncology Oregon
0 Oncology New York

How do create lists of items for every unique ID in a Pandas DataFrame? [duplicate]

This question already has answers here:
How to get unique values from multiple columns in a pandas groupby
(3 answers)
Python pandas unique value ignoring NaN
(4 answers)
Closed 1 year ago.
Imagine I have a table that looks like this.
original table
How do I convert it into this?
converted table
Attached sample data. Thanks.

how do I split 1 str column into 2 columns in a pandas dataframe [duplicate]

This question already has answers here:
How to split a dataframe string column into two columns?
(11 answers)
Closed 2 years ago.
enter image description here
df['business location'] #column i want to split into 2 columns :
df['longitude'] #and
df['latitude']
df[['longitude','latitude']] = sf['Business Location'].str.split(',')
is giving me error:
ValueError: Must have equal len keys and value when setting with an iterable
how do I split?
This will work
df.assign(
longitude=lambda x: x['Business Location'].str.split(',').str[0],
latitude=lambda x: x['Business Location'].str.split(',').str[1])

How to use nlasgest in pandas? [duplicate]

This question already has answers here:
Pandas max value index
(3 answers)
Closed 2 years ago.
I'm looking for the highest row of a dataframa, actually the idea is to pick the highest value and the index. I'm trying to use this code:
data_q11.nlargest(144,['1980','2010'])
where data_q11 is the dataframe,144 the number os rows in this df and range of columns.
Although the result is returning a empty list of 0 rows and x 31 columns.
There is a function in Pandas for the index of the maximum value:
data_q11['col'].idxmax(axis=1)

pandas dataframe to list(just care about data) [duplicate]

This question already has answers here:
Pandas DataFrame to List of Lists
(14 answers)
Closed 5 years ago.
I have a dataframe like this:
how can i get the list like:
[[2017-08-01,1.18263,1.18266,1.18109,1.18113],
......,
[2017-08-18,1.18263,1.18266,1.18109,1.18113]]
DataFrame.values returns the data as a numpy array, from there you can go to a list by adding tolist()