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

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()

Related

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.

what does data[:,0] or data[:,1] mean in pandas [duplicate]

This question already has answers here:
Extracting specific columns in numpy array
(10 answers)
Closed 2 years ago.
I am new to pandas and I saw some pandas code which says
plt.scatter(data[:,0],data[:,1])
I want to know what does data[:,0] and data[:,1] means?
Thas mean:
data[:,0] - > All the first column of the dataset
data[:,1] -> All the second colum of the dataset

Extracting information from Pandas dataframe column headers [duplicate]

This question already has answers here:
Find column whose name contains a specific string
(8 answers)
Closed 2 years ago.
I have a pandas dataframe with column headers, which contain information. I want to loop through the column headers and use logical operations on each header to extract the columns with the relevant information that I have.
my df.columns command gives something like this:
['(param1:x)-(param2:y)-(param3:z1)',
'(param1:x)-(param2:y)-(param3:z2)',
'(param1:x)-(param2:y)-(param3:z3)']
I want to select only the columns, which contain (param3:z1) and (param3:z3).
Is this possible?
You can use filter:
df = df.filter(regex='z1|z3')

How to convert ndarray to pandas DataFrame [duplicate]

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

unable to convert groupby dataset to json in pandas [duplicate]

This question already has answers here:
How to reset index in a pandas dataframe? [duplicate]
(3 answers)
Closed 4 years ago.
I have group by data set but I'm unable to convert it to json. It throws out json with a bad format. TO_excel works fine.
Country Sub amount
3 source4
UK 1 source3
1 source1
US 2 source2
How can I export groupby dataset to_json?
There is problem you have MultiIndex in DataFrame, so need reset_index:
j = df.reset_index().to_json()
print (j)
{"Country":{"0":"UK","1":"UK","2":"US"},
"Sub":{"0":1,"1":1,"2":2},
"amount":{"0":"source3","1":"source1","2":"source2"}}