1、使用iteritems()或items()
1)Python2中使用iteritems()
import pandas as pd
d={u'2022-04-22': 10,
u'2022-04-23': 20,
u'2022-04-24': 30,
u'2022-04-25': 40,
u'2022-04-26': 50,}
print(pd.DataFrame(d.iteritems(), columns=['Date', 'DateValue']))
2)Python3中使用items()
import pandas as pd
d={'2022-04-22': 10,
'2022-04-23': 20,
'2022-04-24': 30,
'2022-04-25': 40,
'2022-04-26': 50,}
print(pd.DataFrame(d.items(), columns=['Date', 'DateValue']))
2、将key做列名
import pandas as pd
d = {'C': '11', 'Java': '22', 'Python': '33',"CJavaPy":'66'}
print(pd.DataFrame([d]))
或
import pandas as pd
d = {'C': '11', 'Java': '22', 'Python': '33',"CJavaPy":'66'}
df = pd.DataFrame()
df = df.append(d, ignore_index=True)
print(df)
3、使用pandas.DataFrame.from_dict()
参考文档:pandas.DataFrame.from_dict
import pandas as pd
d={'2022-04-22': 10,
'2022-04-23': 20,
'2022-04-24': 30,
'2022-04-25': 40,
'2022-04-26': 50,}
df = pd.DataFrame.from_dict(d, orient='index').reset_index()
df.columns = ['Date', 'DateValue']
print(df)
4、使用DataFrame对象的append()方法
import pandas as pd
d={'2022-04-22': 10,
'2022-04-23': 20,
'2022-04-24': 30,
'2022-04-25': 40,
'2022-04-26': 50,}
df = pd.DataFrame()
df = df.append(d, ignore_index=True)
print(df)
5、使用keys()和values()
import pandas as pd
d={'2022-04-22': 10,
'2022-04-23': 20,
'2022-04-24': 30,
'2022-04-25': 40,
'2022-04-26': 50}
print(pd.DataFrame({'Date': d.keys() , 'Date_Value': d.values() }))
或者
import pandas as pd
d={'2022-04-22': 10,
'2022-04-23': 20,
'2022-04-24': 30,
'2022-04-25': 40,
'2022-04-26': 50}
df = pd.DataFrame()
df['Date'] = d.keys()
df['DateValue'] = d.values()
print(df)