简体   繁体   English

在Pandas中创建DataFrame时填写默认值0

[英]Fill with default 0's when creating a DataFrame in Pandas

I have an input dict-of-string-to-list with possibly different lengths for the list. 我有一个输入dict-of-string-to-list,列表可能有不同的长度。

d = {'b': [2,3], 'a': [1]}

when I do: df = pd.DataFrame(data=d) , i'm seeing ValueError: arrays must all be same length 当我这样做: df = pd.DataFrame(data=d) ,我看到ValueError:数组必须全长相同

Question : How do i fill the missing values with default (eg 0) when creating the df? 问题 :如何在创建df时使用默认值(例如0)填充缺失值?


The reason to create the df is to get the final result of: {'b': 3} 创建df的原因是为了获得最终结果: {'b': 3}

whereas 3 is the max of all numbers in the lists. 3是列表中所有数字的最大值。

You can use DataFrame.from_dict setting orient to index so the keys of the dictionary are used as indices and the missing values are set to NaN . 您可以使用DataFrame.from_dict设置orientindex因此字典的键用作索引,缺失值设置为NaN Then simply fill NaNs using .fillna and transpose to set the keys as columns: 然后使用.fillna填充NaNs并转置以将键设置为列:

pd.DataFrame.from_dict(d, orient='index').fillna(0).T

    b    a
0  2.0  1.0
1  3.0  0.0
d = {'b': [2,3], 'a': [1]}
df = pd.DataFrame({ k:pd.Series(v) for k, v in d.items() })

This will give the following output. 这将给出以下输出。

a  b
0  1.0  2
1  NaN  3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM