Python change name of panda dataframe
You've done what you want already.
This line
df2 = df
makes a copy of the reference to df
so both variables point at the same dataframe. It does not copy the dataframe. To verify this:
>>> df2 is df
True
After that, del df
does not delete the dataframe, it deletes the variable that is pointing to it.
How can I change the name of a data frame
The truth is that objects in R don't have names per-se. There exists different kinds of environments, including a global one for every process. These environments have lists of names, that point to various objects. Two different names can point to the same object. This is best explained to my knowledge in the environments chapter of Hadley Wickhams Advanced R book
http://adv-r.had.co.nz/Environments.html
So there is no way to change a name of a data frame, because there is nothing to change.
But you can make a new name (like newname
) point to the same object (in your case a data frame object) as an given name (like oldname
) simply by doing:
newname <- oldname
Note that if you change one of these variables a new copy will be made and the internal references will no longer be the same. This is due to R's "Copy on modify" semantics. See this post for an explanation: What exactly is copy-on-modify semantics in R, and where is the canonical source?
Hope that helps. I know the pain. Dynamic and functional languages are different than static and procedural languages...
Of course it is possible to calculate a new name for a dataframe and register it in the environment with the assign
command - and perhaps you are looking for this. However referring to it afterwards would be rather convoluted.
Example (assuming df
is the dataframe in question):
assign( paste("city_stats", city_code, sep = ""), df )
As always see the help for assign
for more information http://stat.ethz.ch/R-manual/R-devel/library/base/html/assign.html
Edit:
In reply to your edit, and various comments around the problems with using eval(parse(...)
you could parse the name like this:
head(get(gear_subset))
Changing category names in a pandas data frame
TL;DR
Use
Series.cat.rename_categories
for categorical variables.Use
Series.map
for non-categorical variables.Use
Series.replace
if regex is needed.
1. Series.cat.rename_categories
This option is fastest but requires the Categorical
dtype. If you're analyzing categorical variables, this is highly recommended for its speed/memory/semantic benefits.
First convert to Categorical
(if not already):
df['Label'] = df['Label'].astype('category')
Then rename via Series.cat.rename_categories
:
df['Label'] = df['Label'].cat.rename_categories({'zero': 0, 'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6, 'seven': 7, 'eight': 8, 'nine': 9})
# File Label
# 20936 eight/b63fea9e_nohash_1.wav 8
# 21016 eight/f44f440f_nohash_2.wav 8
# 7423 three/d8ed3745_nohash_0.wav 3
# ... ... ...
# 646 zero/24632875_nohash_0.wav 0
2. Series.map
If you can't (or don't want to) use the Categorical
dtype, Series.map
is the next fastest:
df['Label'] = df['Label'].map({'zero': 0, 'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6, 'seven': 7, 'eight': 8, 'nine': 9})
# File Label
# 20936 eight/b63fea9e_nohash_1.wav 8
# 21016 eight/f44f440f_nohash_2.wav 8
# 7423 three/d8ed3745_nohash_0.wav 3
# ... ... ...
# 646 zero/24632875_nohash_0.wav 0
3. Series.replace
This option is slow but offers regex/filling capabilities via the regex
and method
params.
As a contrived example, say we want less granular labels:
mapping = {
r'zero|one': '0,1',
r'two|three': '2,3',
r'four|five': '4,5',
r'six|seven': '6,7',
r'eight|nine': '8,9',
}
Then we can use Series.replace
with regex=True
:
df['Label'] = df['Label'].replace(mapping, regex=True)
# File Label
# 20936 eight/b63fea9e_nohash_1.wav 8,9
# 7423 three/d8ed3745_nohash_0.wav 2,3
# 1103 zero/ad63d93c_nohash_4.wav 0,1
# ... ... ...
# 646 zero/24632875_nohash_0.wav 0,1
Renaming column names in Pandas
Just assign it to the .columns
attribute:
>>> df = pd.DataFrame({'$a':[1,2], '$b': [10,20]})
>>> df
$a $b
0 1 10
1 2 20
>>> df.columns = ['a', 'b']
>>> df
a b
0 1 10
1 2 20
How to rename dataframe in R?
You must use assign
to create an object with a name given by a character string and use get
to get the object given by that string.
Note that the name with the prefix "supp"
will only exist in the function and is discarded on exit.
test <- function(inds){
a1 <- deparse(substitute(inds))
a2 <- paste0("supp", a1)
assign(a2, inds)
out_df <- get(a2)
print(class(a1))
print(class(a2))
print(class(out_df))
out_df
}
head(test(iris))
#> [1] "character"
#> [1] "character"
#> [1] "data.frame"
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.1 3.5 1.4 0.2 setosa
#> 2 4.9 3.0 1.4 0.2 setosa
#> 3 4.7 3.2 1.3 0.2 setosa
#> 4 4.6 3.1 1.5 0.2 setosa
#> 5 5.0 3.6 1.4 0.2 setosa
#> 6 5.4 3.9 1.7 0.4 setosa
Created on 2022-03-23 by the reprex package (v2.0.1)
Related Topics
R Package Xtable, How to Create a Latextable with Multiple Rows and Columns from R
How to Plot a Subset of a Data Frame in R
How to Install Multiple Packages
How to Tell What Packages You Have Used in R
Formatting Ggplot2 Axis Labels with Commas (And K? Mm) If I Already Have a Y-Scale
Knitr: Run All Chunks in an Rmarkdown Document
Make Dataframe of Top N Frequent Terms for Multiple Corpora Using Tm Package in R
Change the Index Number of a Dataframe
How to Convert Ensembl Id to Gene Symbol in R
How Does the Removesparseterms in R Work
Remove Strip Background Keep Panel Border
How to Create Textarea as Input in a Shiny Webapp in R
Comparison Between Dplyr::Do/Purrr::Map, What Advantages
How to Calculate Wind Direction from U and V Wind Components in R