How to convert a table to a data frame
I figured it out already:
as.data.frame.matrix(mytable)
does what I need -- apparently, the table needs to somehow be converted to a matrix in order to be appropriately translated into a data frame. I found more details on this as.data.frame.matrix() function for contingency tables at the Computational Ecology blog.
How to convert a table using pandas.dataframe?
You should get what you want if you use the following code:
new_df = pd.DataFrame(df.unstack()).transpose()
print(new_df)
This works by converting your original df to a series with the MultiIndex of all current columns + the year via pd.DataFrame.unstack, then converting the resultant series back into a dataframe with one row by feeding that series into pd.DataFrame (resulting in one long column, titled 0), then using pd.transpose to transpose that column to be one long row as you intended.
convert table to data frame with the first column as a variable - R
df <- as.data.frame(table)
df$ID <- rownames(table)
safely turn a data.table back into a data.frame
The as.data.frame
method for data.tables is presumably the safest function to use. (Try typing getAnywhere("as.data.frame.data.table")
to see exactly what it does.)
library(data.table)
DT <- data.table(a=1:4, b=letters[c(1,1,2,2)], key="a")
class(as.data.frame(DT)) ## OR: as(X, "data.frame")
# [1] "data.frame"
Convert a data frame to a data.table without copy
This is available from v1.9.0+. From NEWS:
o Following this S.O. post, a function
setDT
is now implemented that takes alist
(named and/or unnamed),data.frame
(ordata.table
) as input and returns the same object as adata.table
by reference (without any copy). See?setDT
examples for more.
This is in accordance with data.table
naming convention - all set*
functions modifies by reference. :=
is the only other that also modifies by reference.
require(data.table) # v1.9.0+
setDT(data) # converts data which is a data.frame to data.table *by reference*
See history for older (now outdated) answers.
Convert HTML Table to Pandas Data Frame in Python
You can just Use pandas read_html function for that, and remember to convert the html you get to string else you will get some parsing error.
import requests
from bs4 import BeautifulSoup
import pandas as pd
url = 'http://my-trade.in/'
page = requests.get(url)
soup = BeautifulSoup(page.content, 'html.parser')
tbl = soup.find("table",{"id":"MainContent_dataGridView1"})
data_frame = pd.read_html(str(tbl))[0]
Convert data.table to data.frame (i.e. undo setDT)
Just like setDT()
converts its input to a data table by reference, there is also setDF()
which converts its input to a data frame by reference. So just call
setDF(df)
and you are back to a data frame with no copies made.
R Convert group_by table to new data frame
This is a simple pivoting task, you can do it with tidyr
's pivot_wider()
function
library(dplyr)
library(tidyr)
mtcars %>%
group_by(cyl, gear) %>%
summarise(HP = sum(hp)) %>%
pivot_wider(gear, names_from = "cyl", values_from = "HP")
# A tibble: 3 x 4
gear `4` `6` `8`
<dbl> <dbl> <dbl> <dbl>
1 3 97 215 2330
2 4 608 466 NA
3 5 204 175 599
Related Topics
Calculate the Mean For Each Column of a Matrix in R
How to Count Runs in a Sequence
Remove Columns from Dataframe Where All Values Are Na
Quit and Restart a Clean R Session from Within R
R - Concatenate Two Dataframes
Filtering Out Duplicated/Non-Unique Rows in Data.Table
Rolling Mean (Moving Average) by Group/Id With Dplyr
Repeat Rows of a Data.Frame N Times
Multiple Use of the Positional '$' Operator to Update Nested Arrays
How to Insert Elements into a Vector
Select Equivalent Rows [A-B & B-A]
Why Is Rbindlist "Better" Than Rbind
How to Divide Each Row of a Matrix by Elements of a Vector in R
Extract Month and Year from a Zoo::Yearmon Object
Change Bar Plot Colour in Geom_Bar With Ggplot2 in R