![](/img/trans.png)
[英]pandas dataframe from_dict - Set column name for key, and column name for key-value
[英]Set column names in pandas data frame from_dict with orient = 'index'
我已經看過這個問題: pandas create named columns in dataframe from dict 。 但是,我的示例略有不同。
我有一本字典: my_dict = {'key1' : [1,2,3], 'key2' : [4,5,6], 'key3' :[7,8,9]}
我創建了一個熊貓數據df = pd.DataFrame.from_dict(my_dict, orient='index')
: df = pd.DataFrame.from_dict(my_dict, orient='index')
,它是面向行的。 但是,在編寫columns = ['one', 'two', 'three']
出現錯誤,如上面的鏈接所示。
我如何命名它們?
是否有理由不能在下一行設置列名?
my_dict = {'key1' : [1,2,3], 'key2' : [4,5,6], 'key3' :[7,8,9]}
df = pd.DataFrame.from_dict(my_dict, orient='index')
df.columns = ['one', 'two', 'three']
應該管用。
從版本 0.23.0 開始,您可以在from_dict
指定一個columns
參數:
my_dict = {'key1': [1, 2, 3], 'key2': [4, 5, 6], 'key3': [7, 8, 9]}
df = pd.DataFrame.from_dict(my_dict, orient='index', columns=['one', 'two', 'three'])
my_dict = {'key1' : [1,2,3], 'key2' : [4,5,6], 'key3' :[7,8,9]}
df = pd.DataFrame.from_dict(my_dict, orient='columns')
df.columns = ['one', 'two', 'three']
將方向從索引更改為列。 這對我有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.