2

I am a very beginner in using pandas and Dataframes.

This is the dictionary :

names_values = {"Alex":[1,2,3,4,5],"Sam":[5,6,7,8,9]}

I want the Dataframe to look like this :

   name  value
0  Alex    1.0
1  Alex    2.0
2  Alex    3.0
3  Alex    4.0
4  Alex    5.0
5   Sam    5.0
6   Sam    6.0
7   Sam    7.0
8   Sam    8.0
9   Sam    9.0

My approach is :

df = pd.DataFrame({"name":[],"value":[]})

for name in names_values.keys():
    for i in range(len(names_values[name])):
        df = df.append({"name": name, "value": names_values[name][i]}, ignore_index=True)

This gave me the result i want, but i am wondering if there is a generic/more efficient way to get the same result.

Thanks

3
  • without range(len()) - for name, values in names_values.items(): for number in values: df = df.append(... "value": number, ...) Commented Jan 7, 2020 at 4:12
  • df append is quite expensive ... Commented Jan 7, 2020 at 4:13
  • it’s probably best to modify your dictionary ahead of time instead of appending one row at a time to your dataframe since that can be slow as data size grows Commented Jan 7, 2020 at 4:14

2 Answers 2

4

One way using pandas.Dataframe.melt:

df = pd.DataFrame(names_values).melt(var_name='name')
print(df)

Output:

   name  value
0  Alex      1
1  Alex      2
2  Alex      3
3  Alex      4
4  Alex      5
5   Sam      5
6   Sam      6
7   Sam      7
8   Sam      8
9   Sam      9
Sign up to request clarification or add additional context in comments.

1 Comment

whoa cool ... i had no idea about this .. i was gonna make N dataframes and concat once ... but this is way better
1

You can also use itertools.cycle to modify your dictionary and then load it in.

from itertools import cycle

cols = ['name', 'value']
d = []

for k, v in names_values.items():
    vals = zip(cycle([k]), v)
    d.extend(list(vals))

pd.DataFrame(d, columns=cols)

   name  value
0  Alex      1
1  Alex      2
2  Alex      3
3  Alex      4
4  Alex      5
5   Sam      5
6   Sam      6
7   Sam      7
8   Sam      8
9   Sam      9

Another option is itertools.repeat

from itertools import repeat

for k, v in names_values.items():
    vals = zip(repeat(k), v)
    d.extend(list(vals))

...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.