简体   繁体   English

创建数据框,并为每个字符串的行显示在另一个数据框的列中

[英]Create Dataframe with rows made for each string appearing in a column of another Dataframe

If you have a dataframe as follows: 如果您具有如下数据框:

genre                 mean_average_budget
horror thriller       x
romance comedy        y 
action thriller       z
documentary           a
comedy documentary    b

How could one be made in which the rows are the individual appearances of each string in the genre column? 怎样才能使其中的行是流派列中每个字符串的单独出现? Eg: 例如:

genre                 mean_average_budget
horror                h
thriller              i 
action                k
documentary           l
comedy                m

Try this 尝试这个

new_df = df.set_index('mean_average_budget').genre.str.split().\
    apply(pd.Series).stack().reset_index(1,drop = True).\
    reset_index(name = 'genre')

    mean_average_budget genre
0   x                   horror
1   x                   thriller
2   y                   romance
3   y                   comedy
4   z                   action
5   z                   thriller
6   a                   documentary
7   b                   comedy
8   b                   documentary

To find mean, try this for numeric data 要查找均值,请尝试此操作以获取数值数据

new_df.groupby('genre')['mean_average_budget'].mean()

If you want to aggregate the strings 如果要汇总字符串

new_df.groupby('genre')['mean_average_budget'].apply('+'.join)

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

相关问题 将数据框中的列的行创建为另一个数据框中的列 - Create rows of a column in a dataframe as a column in another dataframe 创建一个由来自另一个的所有行对组成的 DataFrame - Create a DataFrame which is made of all pairs of rows from another pandas:通过将 DataFrame 行与另一个 DataFrame 的列进行比较来创建新列 - pandas: Create new column by comparing DataFrame rows with columns of another DataFrame 从数据框的两列创建一个新列,其中每列的行包含字符串格式的列表 - Create a new column from two columns of a dataframe where rows of each column contains list in string format 将数据帧插入到另一个数据帧中每个组的行中 - insert dataframe into rows for each group in another dataframe 创建一个列,该列是每行中具有最大长度的字符串 - Pandas Dataframe - Create a column which is string who has the max length within each rows - Pandas Dataframe 创建一个新的 dataframe 列作为另一列的所有行的乘积 - Create a new dataframe column as a product of all the rows of another column dataframe 的行乘以另一个 dataframe 的列 - Rows of dataframe times column of another dataframe 从另一个 dataframe 的某些行创建 dataframe - Create a dataframe from some rows of another dataframe 循环每一列并匹配该值,然后创建另一个 dataframe - Loop each column and match the value then create another dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM