简体   繁体   English

如何使数据框中的每一行每一列都有一个值?

[英]How to make each row in dataframe have one value for each column?

I have the following dataframe which has the columns ID_x and ID_y that contain data separated with a single space: 我有以下数据ID_x ,其中的ID_xID_y列包含用单个空格分隔的数据:

df = pd.DataFrame({
    'fruit':['apple','orange','banana'],
    'ID_x' : ['1 2 3','4','5'],  
    'ID_y' : ['A B', 'C D','E']
    }, index=['0','1','2'])

在此处输入图片说明

I want to split each value in the columns ( ID_x and ID_y ) and create new rows such that each row represents one-to-one correspondence of the split values. 我想拆分列( ID_xID_y )中的每个值并创建新行,以使每一行代表拆分值的一一对应关系。

Something like this: 像这样:

在此处输入图片说明

Any idea how to tackle this problem? 任何想法如何解决这个问题?

What I have tried so far splitting the values in the columns: 到目前为止,我已经尝试过在列中拆分值的方法:

col_x = 'ID_x'
col_y = 'ID_y'

df = df_unflat.assign(**{col_x:df_unflat[col_x].str.split(' ')})
df = df_unflat.assign(**{col_y:df_unflat[col_y].str.split(' ')})

Try this way out: 尝试这种方法:

import pandas as pd
df = pd.DataFrame({
    'fruit':['apple','orange','banana'],
    'ID_x' : ['1 2 3','4','5'],  
    'ID_y' : ['A B', 'C D','E']
    }, index=['0','1','2'])
id_x = df['ID_x'].str.split(' ').apply(Series, 1).stack()
id_y = df['ID_y'].str.split(' ').apply(Series, 1).stack()
id_x.index = id_x.index.droplevel(-1)
id_y.index = id_y.index.droplevel(-1)
id_x.name = 'ID_x'
id_y.name = 'ID_y'
del df['ID_x']
del df['ID_y']
df = df.join(id_x)
df = df.join(id_y)
df.reset_index(drop=True)

Output: 输出:

    fruit   ID_x    ID_y
0   apple   1       A
1   apple   1       B
2   apple   2       A
3   apple   2       B
4   apple   3       A
5   apple   3       B
6   orange  4       C
7   orange  4       D
8   banana  5       E
import itertools
#convert DF values to a numpy array, get all combinations between ID_x, ID_y and fruit, finally reconstruct the Dataframe.    
pd.DataFrame(sum([list(itertools.product(e[0].split(),e[1].split(),[e[2]])) for e in df.values],[]), columns=df.columns)
Out[483]: 
  ID_x ID_y   fruit
0    1    A   apple
1    1    B   apple
2    2    A   apple
3    2    B   apple
4    3    A   apple
5    3    B   apple
6    4    C  orange
7    4    D  orange
8    5    E  banana

暂无
暂无

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

相关问题 如何制作 0 和 1 的 dataframe 使得每个唯一值都是一列? - How to make a dataframe of 0 and 1 such that each unique value is a column? 如何为特定列的每个不同值选择一行并合并以在 Python 中形成新的数据框? - How to select one row for each distinct value for a particular column and merge to form a new dataframe in Python? 如何将 function 应用于 pandas dataframe 中一列的每一行? - How to apply a function to each row of one column in a pandas dataframe? 如何迭代每一行并从一个 dataframe 的特定列中找到下一个匹配列值并将其与另一个 dataframe 进行比较? - How to iterate each row and find the next matching column value from a specific column from one dataframe and comparing it to another dataframe? 如何为数据框中的每一行分配一个值到不同的列? - How can I assign a value to a different column for each row in a dataframe? 如何在数据框中拆分一列并将每个值存储为新行(以熊猫为单位)? - How to split a column in a dataframe and store each value as a new row (in pandas)? 访问每一行并检查数据框中的每一列值 - Acces each row and check each column value in dataframe 如何为 Pandas 数据帧的每一行制作直方图? - How to make a histogram for each row of a Pandas dataframe? 如何使 DataFrame 的每一行成为一个数组? - How to make each row of a DataFrame an array? 将一个 dataframe 的每一行乘以第二个 dataframe 中列的所有行 - Multiply each row of one dataframe by all rows of column in second dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM