python : Create a dataframe from existing pandas dataframe

标签 python python-3.x pandas

现在,我的数据集如下所示:

tconst  Actor1  Actor2  Actor3  Actor4  Actor5  Actor6  Actor7  Actor8  Actor9  Actor10
0   tt0000001   NaN GreaterEuropean, WestEuropean, French   GreaterEuropean, British    NaN NaN NaN NaN NaN NaN NaN
1   tt0000002   NaN GreaterEuropean, WestEuropean, French   NaN NaN NaN NaN NaN NaN NaN NaN
2   tt0000003   NaN GreaterEuropean, WestEuropean, French   GreaterEuropean, WestEuropean, French   GreaterEuropean, WestEuropean, French   NaN NaN NaN NaN NaN NaN
3   tt0000004   NaN GreaterEuropean, WestEuropean, French   NaN NaN NaN NaN NaN NaN NaN NaN
4   tt0000005   NaN GreaterEuropean, British    GreaterEuropean, British    NaN NaN NaN NaN NaN NaN NaN

我使用替换和映射功能到达这里。

Another Look to Dataset

我想从上面的数据帧创建一个数据帧,例如我可以获得如下结果数据帧。

tconst  GreaterEuropean   WestEuropean   French  GreaterEuropean   British    Arab    British   ............
tt0000001   2   1   0   4   1   0   2 .....
tt0000002   0   2   4   0   1   3   0 .....

GreaterEuropean English WestEuropean Italian French ...代表由 tconst 指定的特定电影中不同 Actor 的种族数量。

这就像一个计数矩阵,例如一部电影 tt00001 有 5 个阿拉伯人、2 个英国人、1 个西欧人等等,这样一部电影中有多少个 Actor 属于这些种族。 链接至数据 - https://drive.google.com/open?id=1oNfbTpmLA0imPieRxGfU_cBYVfWN3tZq

最佳答案

import numpy as np
import pandas as pd

df_melted = pd.melt(df, id_vars = 'tconst', 
                    value_vars = df.columns[2:].tolist(), 
                    var_name = 'actor', 
                    value_name = 'ethnicities').dropna()

print(df_melted.ethnicities.str.get_dummies(sep = ',').sum())

输出:

 British               169
 EastAsian               9
 EastEuropean           17
 French                 73
 Germanic                9
 GreaterEastAsian       13
 Hispanic                9
 IndianSubContinent      2
 Italian                 7
 Japanese                4
 Jewish                 25
 Nordic                  7
 WestEuropean          105
Asian                   15
GreaterEuropean        316
dtype: int64

这接近您想要的,但不准确。要获得您想要的内容,而不键入列或值的列表,会更加复杂。

来自:https://stackoverflow.com/a/48120674/6672746

def change_column_order(df, col_name, index):
    cols = df.columns.tolist()
    cols.remove(col_name)
    cols.insert(index, col_name)
    return df[cols]

def split_df(dataframe, col_name, sep):
    orig_col_index = dataframe.columns.tolist().index(col_name)
    orig_index_name = dataframe.index.name
    orig_columns = dataframe.columns
    dataframe = dataframe.reset_index()  # we need a natural 0-based index for proper merge
    index_col_name = (set(dataframe.columns) - set(orig_columns)).pop()
    df_split = pd.DataFrame(
        pd.DataFrame(dataframe[col_name].str.split(sep).tolist())
        .stack().reset_index(level=1, drop=1), columns=[col_name])
    df = dataframe.drop(col_name, axis=1)
    df = pd.merge(df, df_split, left_index=True, right_index=True, how='inner')
    df = df.set_index(index_col_name)
    df.index.name = orig_index_name
    # merge adds the column to the last place, so we need to move it back
    return change_column_order(df, col_name, orig_col_index)

使用这些优秀的功能:

df_final = split_df(df_melted, 'ethnicities', ',')
df_final.set_index(['tconst', 'actor'], inplace = True)
df_final.pivot_table(index = ['tconst'], 
                     columns = 'ethnicities', 
                     aggfunc = pd.Series.count).fillna(0).astype('int')

输出:

ethnicities     British     EastAsian   EastEuropean    French  Germanic    GreaterEastAsian    Hispanic    IndianSubContinent  Italian     Japanese    Jewish  Nordic  WestEuropean    Asian   GreaterEuropean
tconst                                                          
tt0000001   1   0   0   1   0   0   0   0   0   0   0   0   1   0   2
tt0000002   0   0   0   1   0   0   0   0   0   0   0   0   1   0   1
tt0000003   0   0   0   3   0   0   0   0   0   0   0   0   3   0   3
tt0000004   0   0   0   1   0   0   0   0   0   0   0   0   1   0   1
tt0000005   2   0   0   0   0   0   0   0   0   0   0   0   0   0   2

关于 python : Create a dataframe from existing pandas dataframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48549007/

相关文章:

python - Pandas :从MultiIndex中的日期选择

python - 基于行的图表绘制(Seaborn 或 Matplotlib)

python - 有效地将值分配给groupby中的第一行

python - 跨模块共享单例

python - 按下按钮时 Pygame Bool 错误

python - pd.DataFrame.boxplot 与 pd.cut 不兼容

python - 如何根据 Pandas 中的条件现有列创建两列?

python - 考虑到python中的一些条件,如何在字符串中插入一个字符

python - Python 中的搜索/匹配正则表达式

python-3.x - 在词形还原中应用多个 pos 参数