Trying to learn a bit of Pandas and now want to add a index cause prefer it over just numbers, and also wanna learn how to do it ofc, but it won’t work
If I don’t add the index the code works fine and I don’t get NaN values and get the hex codes and RGB codes.
I haven’t found any solucion yet can’t find anything that resolves my problem.
Thanks in advance
When I run this code I only get the index and the rest turns into NaN, why?
import pandas as pd
import numpy as np
def colors():
colordata = pd.DataFrame(
{
"Hex code": [
"#000000",
"#FFFFFF",
"#FF0000",
"#00FF00",
"#0000FF",
"#FFFF00",
"#00FFFF",
"#FF00FF",
"#C0C0C0",
"#808080",
"#800000",
"#808000",
"#008000",
"#800080",
"#008080",
"#000080"
],
"Decimal Code (RGB)": [
"(0,0,0)",
"(255,255,255)",
"(255,0,0)",
"(255,0,0)",
"(0,0,255)",
"(255,255,0)",
"(0,255,255)",
"(255,0,255)",
"(192,192,192)",
"(128,128,128)",
"(128,0,0)",
"(128,128,0)",
"(0,128,0)",
"(128,0,128)",
"(0,128,128)",
"(0,0,128)"
]
}
)
df = pd.DataFrame(colordata, index = [
"Black",
"White",
"Red",
"Lime",
"Blue",
"Yellow",
"Cyan / Aqua",
"Magenta / Fuchsia",
"Silver",
"Gray",
"Maroon",
"Olive",
"Green",
"Purple",
"Teal",
"Navy"
])
print("What color do you want information on?")
print(df)
colors()
Here is what I get when I run the code
What color do you want information on?
Hex code Decimal Code (RGB)
Black NaN NaN
White NaN NaN
Red NaN NaN
Lime NaN NaN
Blue NaN NaN
Yellow NaN NaN
Cyan / Aqua NaN NaN
Magenta / Fuchsia NaN NaN
Silver NaN NaN
Gray NaN NaN
Maroon NaN NaN
Olive NaN NaN
Green NaN NaN
Purple NaN NaN
Teal NaN NaN
Navy NaN NaN
Robin Rouwenhorst is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.