简体   繁体   English

如何转换 python 数据帧,以便将唯一的行值转置到列,另一列的值成为它们的行

[英]How to transform python data frame such that unique row values are transposed to columns and values of another column become their rows

Summary概括
I am using Python 2.7.我正在使用 Python 2.7。 I have a data frame with all categorical variables ie data type is string.我有一个包含所有分类变量的数据框,即数据类型是字符串。 I would like to transform unique row values of one column into multiple columns.我想将一列的唯一行值转换为多列。 Additionally, the values of those resulting columns must have the corresponding values from another column.此外,这些结果列的值必须具有来自另一列的相应值。 To describe in detail, I have provided a reproducible data frame and expected output for your reference.为了详细描述,我提供了一个可重现的数据帧和预期的 output 供您参考。

Dataframe that needs transposing can be created as follows:需要转置的 Dataframe 可以创建如下:

import pandas as pd
codes = ['codeA','codeB', 'codeC']
variables = ['textA','textA','textB']
dataset = list(zip(codes,variables))
df = pd.DataFrame(data = dataset, columns=['codes','variables'])
df['string'] = 'string1'

The data frame that needs transposing looks like this:需要转置的数据框如下所示:

df
   codes variables   string
0  codeA     textA  string1
1  codeB     textA  string1
2  codeC     textB  string1

The expected final output should like this:预期的最终 output 应该是这样的:

textA textB string
codeA       string1
codeB
      codeC string1

Note: The objective is transposition.注意:目标是转置。 I am not overly concerned whether the blank spaces are NULL values or zeroes.我不太担心空格是 NULL 值还是零。

Im not sure about the last column in your example as it seems inconsistent with the rest of the transformation.我不确定您示例中的最后一列,因为它似乎与转换的 rest 不一致。 In any ways, I think converting the variable column using pandas get_dummies function is probably a good place to start.无论如何,我认为使用 pandas get_dummies function 转换变量列可能是一个不错的起点。

import pandas as pd
codes = ['codeA','codeB', 'codeC']
variables = ['textA','textA','textB']
dataset = list(zip(codes,variables))
df = pd.DataFrame(data = dataset, columns=['codes','variables'])
df['string'] = 'string1'

df = pd.get_dummies(df, columns=['variables'])
df.variables_textA = df.codes.where(df.variables_textA.astype(bool),0)
df.variables_textB = df.codes.where(df.variables_textB.astype(bool),0)
columns = ['variables_textA', 'variables_textB','string']
df = df[columns]

结果

暂无
暂无

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

相关问题 如何根据列的唯一值将其他列的行转换为列? - How to transform rows of other columns to columns on the basis of unique values of a column? python - 如何使用python中另一个数据框中的列中的重复值为唯一行对数据框进行子集化? - How can I subset a data frame for unique rows using repeating values from a column in another data frame in python? 将一列的唯一值转换为包含另一列中相应值的多列PYTHON - Transform unique values of a column into multiple columns containing their corresponding values in another column PYTHON 如果满足基于同一数据帧中其他2列的行值的条件,则在数据帧的列行中填充值 - Filling values in rows of column in a data frame, if condition based on 2 other columns row values in the same data frame is met 如何将唯一列值取消堆叠到列并将另一列设置为 Python Pandas 中的行索引 - How to unstack unique column values to columns and set another column as row index in Python Pandas 重塑数据框,使索引列值成为列 - Reshape data frame, so the index column values become the columns 如何根据条件统计所有数据框列值并将列转置为 Python 中的行 - How to count all data frame column values based on condition and transpose the columns into rows in Python 将3列(x,y,结果)Python Pandas DataFrame转换为以x(唯一)为行,y(唯一)为列的结果值的数据帧 - Transform a 3 columns (x, y, result) Python Pandas DataFrame to a DataFrame of result values with x (unique) as row and y (unique) as column 列中每行具有唯一值的 Python/CSV 唯一行 - Python/CSV unique rows with unique values per row in a column Python:通过另一列中的每个唯一行条目对数据框中的多列求和 - Python: Sum multiple columns in a Data Frame by each unique row entry in another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM