相关疑难解决方法(0)

将多个列值合并到python pandas中的一列中

我有一个像这样的pandas数据框:

   Column1  Column2  Column3  Column4  Column5
 0    a        1        2        3        4
 1    a        3        4        5
 2    b        6        7        8
 3    c        7        7        
Run Code Online (Sandbox Code Playgroud)

我现在要做的是获取一个包含Column1和新columnA的新数据帧.此列A应包含第2列的所有值 - (到)n(其中n是从Column2到行尾的列数),如下所示:

  Column1  ColumnA
0   a      1,2,3,4
1   a      3,4,5
2   b      6,7,8
3   c      7,7
Run Code Online (Sandbox Code Playgroud)

我怎样才能最好地解决这个问题?任何意见将是有益的.提前致谢!

python row list dataframe pandas

27
推荐指数
4
解决办法
4万
查看次数

将Pandas DataFrame中的列与DataFrame中的列列相结合

请考虑以下内容DataFrame.

n  v1 v2 v3 v4 v5
0   1  2  3  4  5
1   1  2  3  4  5
2   1  2  3  4  5
Run Code Online (Sandbox Code Playgroud)

对于每一行,我要添加的值v2,v3,v4到列表,并在列表乘值v5,并把结果放入一个新列v6,这样我最终有一个DataFrame是这样的:

n  v1  v6
0   1  [10, 15, 20]
1   1  [10, 15, 20]
2   1  [10, 15, 20]
Run Code Online (Sandbox Code Playgroud)

我怎样才能在熊猫中实现这一目标?

python list dataframe pandas

11
推荐指数
1
解决办法
1万
查看次数

将数据框转换为具有列表值的字典

假设我有一个Dataframe df

Label1    Label2        Label3
key1      col1value1    col2value1
key2      col1value2    col2value2
key3      col1value3    col2value3


dict1 = df.set_index('Label1').to_dict() 
Run Code Online (Sandbox Code Playgroud)

当我们有 2 列时,这有效..

预期输出:

my_dict = {key1: [col1value1,col2value1] , key2: [ col1value2,col2value2] , key3:[col1value3,col2value3] }
Run Code Online (Sandbox Code Playgroud)

我可以to_dict在 Dataframe df上使用一个带有2 个其他列的键作为列表形式的吗??

python dictionary dataframe python-2.7 pandas

6
推荐指数
1
解决办法
1778
查看次数

如何从数据帧创建键:列名和值的字典:python 列中的唯一值

我正在尝试创建一个 key:value 对字典,其中 key 是数据框的列名,value 将是一个包含该列中所有唯一值的列表。最终我希望能够从dict 基于条件。到目前为止,这是我能够做的:

for col in col_list[1:]:
    _list = []
    _list.append(footwear_data[col].unique())
    list_name = ''.join([str(col),'_list'])

product_list = ['shoe','footwear']
color_list = []
size_list = []
Run Code Online (Sandbox Code Playgroud)

这里 product,color,size 都是列名,dict 键应该相应地命名为 color_list 等。最终我需要访问字典中的每个 key:value_list。预期输出:

KEY              VALUE
color_list :    ["red","blue","black"]
size_list:  ["9","XL","32","10 inches"]
Run Code Online (Sandbox Code Playgroud)

有人可以帮我解决这个问题吗?附上数据的快照。数据帧

python dictionary list pandas

5
推荐指数
1
解决办法
9446
查看次数

标签 统计

pandas ×4

python ×4

dataframe ×3

list ×3

dictionary ×2

python-2.7 ×1

row ×1