动态在 Pandas 数据框中添加列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40467327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:22:53  来源:igfitidea点击:

Add columns in pandas dataframe dynamically

pythonpandasdataframe

提问by Ishan Bhatt

I have following code to load dataframe

我有以下代码来加载数据框

import pandas as pd

ufo = pd.read_csv('csv_path')
print ufo.loc[[0,1,2] , :]

which gives following output, see the structure of the csv

给出以下输出,请参阅 csv 的结构

          City Colors Reported Shape Reported State             Time
0       Ithaca             NaN       TRIANGLE    NY   6/1/1930 22:00
1  Willingboro             NaN          OTHER    NJ  6/30/1930 20:00
2      Holyoke             NaN           OVAL    CO  2/15/1931 14:00

Now, I want to add an extra column based on existing column. I have a list which consist of indices of participating columns. It can be 0,1or 0,2,3or 1,2,3anything.

现在,我想根据现有列添加一个额外的列。我有一个由参与列的索引组成的列表。它可以是0,10,2,31,2,3任何东西。

I need to create it dynamically. I could come up with following

我需要动态创建它。我可以想出以下

df1['combined'] = df1['City']+','+df1['State']

Putting index doesn't seem to work. I want to join those columns. using ','.join()

放置索引似乎不起作用。我想加入那些专栏。使用','.join()

采纳答案by Psidom

Assuming the data types of all the columns you want to joinare str, you can use []with integer to pick up the columns and use applyto join them:

假设您想要的所有列的数据类型join都是str,您可以使用[]with integer 来获取列并用于apply连接它们:

df[[0,2,3]].apply(','.join, axis=1)

#0      Ithaca,TRIANGLE,NY
#1    Willingboro,OTHER,NJ
#2         Holyoke,OVAL,CO
#dtype: object

回答by Ami Tavory

If the list of indices is l, you can use pd.Series.cat:

如果索引列表是l,则可以使用pd.Series.cat

df[df.columns[l[0]]].astype(str).str.cat([df[df.columns[i]].astype(str) for i in l[1: ]], sep=',')


Example

例子

In [18]: df = pd.DataFrame({'a': [1, 2], 'b': [2, 'b'], 'c': [3, 'd']})

In [19]: df[df.columns[l[0]]].astype(str).str.cat([df[df.columns[i]].astype(str) for i in l[1: ]], sep=',')
Out[19]: 
0    1,2
1    2,b
Name: a, dtype: object

回答by piRSquared

def dyna_join(df, positions):
    return pd.concat([df, df.iloc[:, positions].apply(','.join, 1).rename('new_col')], axis=1)


dyna_join(df, [0, -2])

enter image description here

在此处输入图片说明