Create dataframe from a matrix
If you change your time
column into row names, then you can use as.data.frame(as.table(mat))
for simple cases like this.
Example:
data <- c(0.1, 0.2, 0.3, 0.3, 0.4, 0.5)
dimnames <- list(time=c(0, 0.5, 1), name=c("C_0", "C_1"))
mat <- matrix(data, ncol=2, nrow=3, dimnames=dimnames)
as.data.frame(as.table(mat))
time name Freq
1 0 C_0 0.1
2 0.5 C_0 0.2
3 1 C_0 0.3
4 0 C_1 0.3
5 0.5 C_1 0.4
6 1 C_1 0.5
In this case time and name are both factors. You may want to convert time back to numeric, or it may not matter.
Creating a Pandas DataFrame from a Numpy array: How do I specify the index column and column headers?
You need to specify data
, index
and columns
to DataFrame
constructor, as in:
>>> pd.DataFrame(data=data[1:,1:], # values
... index=data[1:,0], # 1st column as index
... columns=data[0,1:]) # 1st row as the column names
edit: as in the @joris comment, you may need to change above to np.int_(data[1:,1:])
to have correct data type.
How to create the matrix from data frame?
We can use acast
library(reshape2)
acast(df1, year~site, value.var="SLP", mean)
Or using tapply
from base R
with(df1, tapply(SLP, list(year, site), FUN = mean))
How to convert matrix to pandas data frame
As already said your are not creating a matrix but a python dictionary. However a dict can serve as parameter to create a dataframe, but you reversed the indexing order.
import pandas as pd
matrixA={}
matrixA['a']=[0,0]
matrixA['b']=[0,1]
pd.DataFrame(matrixA)
a b
0 0 0
1 0 1
Additionally you can use numpys matrix
import numpy as np
a = np.matrix('1 2; 3 4')
pd.DataFrame(a)
0 1
0 1 2
1 3 4
How to create dataframe based on matrix?
Set index and columns names by df1, df2
:
res.index = df1[:len(res.index)]
res.columns = df2[:len(res.columns)]
And then reshape by DataFrame.melt
:
df = res.rename_axis(index='df1', columns='df2').melt(ignore_index=False)
Or DataFrame.stack
:
df = res.rename_axis(index='df1', columns='df2').stack().reset_index(name='value')
Create a dataframe from arrays python
This should do the trick for you.
columns = ["A", "B", "C"]
rows = ["D", "E", "F"]
data = np.array([[1, 2, 2], [3, 3, 3],[4, 4, 4]])
df = pd.DataFrame(data=data, index=rows, columns=columns)
Related Topics
Finding the Maximum Value for Each Row Among 3 Columns in R
Merge Data Frames Based on Rownames in R
How to Multiply Data Frame by Vector
How to Change Order of Boxplots When Using Ggplot2
R Error "Sum Not Meaningful for Factors"
Split Dataframe by Levels of a Factor and Name Dataframes by Those Levels
Add an Index (Numeric Id) Column to Large Data Frame
Conditionally Display a Block of Text in R Markdown
Agrep: Only Return Best Match(Es)
Changing the Line Type in the Ggplot Legend
Replace Values in a Vector Based on Another Vector
Dplyr: Lead() and Lag() Wrong When Used with Group_By()
Extract Names of Objects from List
Subset Based on Variable Column Name
How to Change the First Row to Be the Header in R
Increase Resolution of Color Scale for Values Close to Zero