Flatten of dict of lists into a dataframe
问题 I have a dict of lists say: data = {'a': [80, 130], 'b': [64], 'c': [58,80]} How do I flatten it and convert it into dataframe like the one below: 回答1: Use nested list comprehension with if-else if want no count one element lists: df = pd.DataFrame([('{}{}'.format(k, i), v1) if len(v) > 1 else (k, v1) for k, v in data.items() for i, v1 in enumerate(v, 1)], columns=['Index','Data']) print (df) Index Data 0 a1 80 1 a2 130 2 b 64 3 c1 58 4 c2 80 EDIT: data = {'a': [80, 130], 'b': np.nan, 'c':