Lets say I have the following df:
rs = np.random.RandomState(1979)
x = rs.randn(500)
g = np.tile(list("ABCDEFGHIJ"), 50)
df = pd.DataFrame(dict(x=x, g=g))
m = df.g.map(ord)
df["x"] += m
df
x g
0 64.038123 A
1 66.147050 B
2 66.370011 C
3 68.791019 D
4 70.583534 E
... ... ...
495 69.358022 F
496 72.212877 G
497 70.474247 H
498 73.251022 I
499 74.461828 J
I would Like to add a new column that consists of a number 1-10 that matches the letter of the alphabet, like so:
g new
A 1
B 2
C 3
D 4
The actual data I'm working with uses names instead of alphabet letters, but I obviously don't want to put those on here. But again, my goal is make a new column with a values I want for certain names.
Thanks!
It sounds like a dictionary that maps names to their respective ordered position you want in your graph is the way to go. Here's a simple example:
>>> import pandas as pd
>>> df = pd.DataFrame({"Name": ["Alice", "Bob", "Bob", "Alice", "Charlie"]})
>>> ordering = {"Alice": 1, "Bob": 3, "Charlie": 2}
>>> df["Ordering"] = df["Name"].map(ordering)
>>> df
Name Ordering
0 Alice 1
1 Bob 3
2 Bob 3
3 Alice 1
4 Charlie 2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.