简体   繁体   English

熊猫:即使缺少数据,也为列的每个唯一值创建行

[英]Pandas: create rows for each unique value of a column, even with missing data

Note : I had difficulty wording the title of my question, so if you can think of something better to help other people with a similar question, please let me know and I will change it. 注意 :我很难用措辞来表达我的问题的标题,因此,如果您能想到更好的方法来帮助其他有类似问题的人,请告诉我,我将对其进行更改。

Current Data 当前数据

Stored as a Pandas DataFrame 存储为Pandas DataFrame

print(df)

week | site | vol
1    | a    | 10
2    | a    | 11
3    | a    | 2
1    | b    | 55
2    | b    | 1
1    | c    | 69
2    | c    | 66
3    | c    | 23

Notice that site b has no data for week 3 请注意,网站b没有第3周的数据

Goal 目标

week | site | vol
1    | a    | 10
2    | a    | 11
3    | a    | 2
1    | b    | 55
2    | b    | 1
3    | b    | 0
1    | c    | 69
2    | c    | 66
3    | c    | 23

Essentially, I want to create rows for all of the unique combinations of week and site . 本质上,我想为weeksite所有唯一组合创建行。 If the original data doesn't have a vol for a week-site combo, then it gets a 0 . 如果原始数据在week-site组合中没有vol ,那么它将获得0

Using stack with unstack 使用stackunstack

df.set_index(['week','site']).unstack('week',fill_value=0).stack().reset_index()
Out[424]: 
  site  week  vol
0    a     1   10
1    a     2   11
2    a     3    2
3    b     1   55
4    b     2    1
5    b     3    0
6    c     1   69
7    c     2   66
8    c     3   23

You can use crosstab and stack : 您可以使用crosstabstack

pd.crosstab(df.site,df.week,df.vol, aggfunc='first').fillna(0).stack().reset_index(name='vol')

Output: 输出:

  site  week   vol
0    a     1  10.0
1    a     2  11.0
2    a     3   2.0
3    b     1  55.0
4    b     2   1.0
5    b     3   0.0
6    c     1  69.0
7    c     2  66.0
8    c     3  23.0

暂无
暂无

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

相关问题 是否有熊猫函数来转置数据框以为现有列的每个唯一值创建单独的列? - Is there a pandas function to transpose a data frame to create a separate column for each unique value of an existing column? 为 Pandas 列中的每个唯一值创建新的数据框 - Create new dataframes for each unique value in a column in pandas pandas 循环遍历列中每个唯一值的数据帧 - pandas loop through data frame for each unique value in column Python Pandas - 过滤 pandas dataframe 以获取一列中具有最小值的行,以获取另一列中的每个唯一值 - Python Pandas - filter pandas dataframe to get rows with minimum values in one column for each unique value in another column Python Pandas 旋转:如何在第一列中分组并为第二列中的每个唯一值创建一个新列 - Python Pandas pivoting: how to group in the first column and create a new column for each unique value from the second column Pandas 计算按列值的唯一行分组 - Pandas Calculation Grouped By Unique Rows of Column Value 对于 Pandas DataFrame 列中的每个唯一值,如何随机选择一定比例的行? - For each unique value in a pandas DataFrame column, how can I randomly select a proportion of rows? Python Pandas - 如何 select 仅针对列的每个唯一值的前 N 行 - Python Pandas - How to select only the first N rows for each unique value of a column 从某个范围内的列中找到一个固定值,并在熊猫数据框中找到另一列的每个唯一值 - find a fix value from a column around a range with each unique values of another column in pandas data frame 熊猫根据列的每个唯一元素添加行 - Pandas add rows according for each unique element of a column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM