简体   繁体   English

pandas-根据数据框的2个单独列中的值构造列

[英]pandas - construct column depending on values in 2 separate columns of dataframe

I have a pandas dataframe which looks similar to this(i have cooked up an example, since I can't share the data) 我有一个看起来与此类似的pandas数据框(我准备了一个示例,因为我无法共享数据)

raw_data = {'regiment': ['Nighthawks', 'Nighthawks', 'Dragoons', 'Dragoons', 'Scouts'], 
        'company': ['1st', '2nd', '1st', '2nd', '2nd'],  
        'thisValue': [1, 2, 3, 2, 7],
        'total': [3, 3, 5, 5, 7]}
df = pd.DataFrame(raw_data, columns = ['regiment', 'company', 'thisValue', 'total'])
df

The output is : 输出为:

    regiment    company thisValue   total
0   Nighthawks  1st         1         3
1   Nighthawks  2nd         2         3
2   Dragoons    1st         3         5
3   Dragoons    2nd         2         5
4   Scouts      2nd         7         7

I want to have statistics about count of values of eachValue for a regiment. 我想获得有关某团的eachValue值计数的统计信息。 That is I need the resulting dataframe to be like this: 那就是我需要结果数据框是这样的:

regiment    1stCompanyValue 2nd_Company_Value   total
Nighthawks         1               2              3
Dragoons           3               2              5
Scouts             0               7              7

I tried grouping it on company values, but then not sure how to proceed. 我尝试将其按公司价值分组,但随后不确定如何进行。 How can this be done in pandas? 如何在大熊猫中做到这一点?

We can make use of pivot , groupby and concat ie 我们可以利用pivotgroupbyconcat

one  = df.pivot(columns='company',values='thisValue',index='regiment').add_suffix('_company_value').fillna(0)
two = df.groupby('regiment')['total'].first()

ndf = pd.concat([one,two],1)

              1st_company_value  2nd_company_value  total
regiment                                               
Dragoons                  3.0                2.0      5
Nighthawks                1.0                2.0      3
Scouts                    0.0                7.0      7

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

相关问题 Pandas-根据特定列的值在DataFrame中创建单独的列 - Pandas - Create Separate Columns in DataFrame Based on a Specific Column's Values 根据相似的列值在单独的熊猫数据框中乘以列 - Multiplying columns in separate pandas dataframe based on similar column values 取决于 Pandas Dataframe 列的值数组 - Array of values depending on column of Pandas Dataframe 大熊猫将列值拆分为单独的列 - pandas split column values into separate columns 从 pandas dataframe 列中的列表中分离 dict 到不同的 dataframe 列 - separate dict from list in pandas dataframe column into different dataframe columns 同时迭代两个pandas数据框列,并将每个列的值返回到单独的位置 - Iterate two pandas dataframe columns at the same time and return values from each column into separate places Pandas DataFrame:根据现有列的值检查将值写入列 - Pandas DataFrame: Writing values to column depending on a value check of existing column 根据熊猫数据框中的内容将一列分为两列 - Split one column to two columns depending one the content in pandas dataframe 根据列的原始数据类型更改 Pandas Dataframe 中列的数据类型 - Change datatype of columns in Pandas Dataframe depending on the original data type of the column 熊猫:替换数据框列中的列值 - Pandas: Replacing column values in dataframe columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM