简体   繁体   English

将数据框列值转换为新列

[英]Convert dataframe column values to new columns

I have a dataframe containing some data, which I want to transform, so that the values of one column define the new columns. 我有一个包含一些数据的数据框,我想要转换,以便一列的值定义新列。

>>> import pandas as pd
>>> df = pd.DataFrame([['a','a','b','b'],[6,7,8,9]]).T
>>> df
   A  B
0  a  6
1  a  7
2  b  8
3  b  9

The values of the column A shall be the column names of the new dataframe. A的值应为新数据帧的列名。 The result of the transformation should look like this: 转换的结果应如下所示:

   a  b
0  6  8
1  7  9

What I came up with so far didn't work completely: 到目前为止我想出的并没有完全发挥作用:

>>> pd.DataFrame({ k : df.loc[df['A'] == k, 'B'] for k in df['A'].unique() })
     a    b
0    6  NaN
1    7  NaN
2  NaN    8
3  NaN    9

Besides this being incorrect, I guess there probably is a more efficient way anyway. 除了这是不正确的,我想无论如何可能还有更有效的方法。 I'm just really having a hard time understanding how to handle things with pandas. 我真的很难理解如何处理大熊猫的事情。

You were almost there but you need the .values as the list of array and then provide the column names. 你几乎就在那里,但你需要.values作为数组列表,然后提供列名。

pd.DataFrame(pd.DataFrame({ k : df.loc[df['A'] == k, 'B'].values for k in df['A'].unique() }), columns=df['A'].unique())

Output: 输出:

    a   b
0   6   8
1   7   9

Use set_index , groupby , cumcount , and unstack : 使用set_indexgroupbycumcount ,并unstack

(df.set_index(['A', df.groupby('A').cumcount()])['B']
   .unstack(0)
   .rename_axis([None], axis=1))

Output: 输出:

   a  b
0  6  8
1  7  9

Using a dictionary comprehension with groupby : 使用groupby的字典理解:

res = pd.DataFrame({col: vals.loc[:, 1].values for col, vals in df.groupby(0)})

print(res)

   a  b
0  6  8
1  7  9

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

相关问题 Pandas:将DataFrame列值转换为新的Dataframe索引和列 - Pandas: Convert DataFrame Column Values Into New Dataframe Indices and Columns DataFrame将列值转换为新列 - DataFrame transform column values to new columns 将MultiIndex名称转换为DataFrame列值并重命名DataFrame列 - Convert MultiIndex Names to DataFrame Column Values & Rename DataFrame Columns Pandas DataFrame - 将列转换为 JSON 并添加为新列 - Pandas DataFrame - Convert columns into JSON and add as a new column 通过解析列值为数据框创建新列,并使用来自另一列python的值填充新列 - Create new columns for a dataframe by parsing column values and populate new columns with values from another column python 如何将具有 n 个值的 DataFrame 列转换为 n 个二进制列? - How to convert column of DataFrame with n values to n binary columns? 将 dataframe 列字符串值转换为虚拟变量列 - Convert dataframe column string values into dummy variable columns 使用 Dask Dataframe 按值计数将一列的行值转换为多列 - Convert Row values of a column into multiple columns by value count with Dask Dataframe 根据Pandas Dataframe中一列中的字符串将值传递给新列 - Passing values to new columns based on string in one column in a Pandas Dataframe 在由现有列中的值组成的数据框中创建新列 - Create a new column in a dataframe consisting of values from existing columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM