your programing

Python Pandas의 기존 DataFrame에 새 열 추가

lovepro 2020. 9. 28. 09:46
반응형

Python Pandas의 기존 DataFrame에 새 열 추가


연속 번호가 아닌 명명 된 열과 행이있는 다음 인덱싱 된 DataFrame이 있습니다.

          a         b         c         d
2  0.671399  0.101208 -0.181532  0.241273
3  0.446172 -0.243316  0.051767  1.577318
5  0.614758  0.075793 -0.451460 -0.012493

'e'기존 데이터 프레임에 새 열을 추가하고 데이터 프레임에서 아무것도 변경하고 싶지 않습니다 (즉, 새 열의 길이는 항상 DataFrame과 동일 함).

0   -0.335485
1   -1.166658
2   -0.385571
dtype: float64

나는 다른 버전을 시도 join, append, merge,하지만 난 대부분에서, 오류 만 원하는 결과를 얻을하지 않았다. e위의 예에 추가 하려면 어떻게 해야합니까?


원래 df1 인덱스를 사용하여 시리즈를 만듭니다.

df1['e'] = pd.Series(np.random.randn(sLength), index=df1.index)

편집 2015
일부는 SettingWithCopyWarning이 코드를 받고 있다고보고했습니다 .
그러나 코드는 현재 pandas 버전 0.16.1에서 여전히 완벽하게 실행됩니다.

>>> sLength = len(df1['a'])
>>> df1
          a         b         c         d
6 -0.269221 -0.026476  0.997517  1.294385
8  0.917438  0.847941  0.034235 -0.448948

>>> df1['e'] = pd.Series(np.random.randn(sLength), index=df1.index)
>>> df1
          a         b         c         d         e
6 -0.269221 -0.026476  0.997517  1.294385  1.757167
8  0.917438  0.847941  0.034235 -0.448948  2.228131

>>> p.version.short_version
'0.16.1'

SettingWithCopyWarning목표는 Dataframe의 복사본을 가능성이 잘못된 할당의 통보합니다. 반드시 잘못했다고 말하지는 않지만 (오탐을 유발할 수 있음) 0.13.0부터 동일한 목적에 더 적합한 방법이 있음을 알려줍니다. 그런 다음 경고가 표시되면 조언을 따르십시오. 대신 .loc [row_index, col_indexer] = value를 사용해보십시오 .

>>> df1.loc[:,'f'] = pd.Series(np.random.randn(sLength), index=df1.index)
>>> df1
          a         b         c         d         e         f
6 -0.269221 -0.026476  0.997517  1.294385  1.757167 -0.050927
8  0.917438  0.847941  0.034235 -0.448948  2.228131  0.006109
>>> 

실제로 이것은 현재 pandas 문서에 설명 된 더 효율적인 방법입니다.


2017 편집

주석과 @Alexander에서 알 수 있듯이 현재 Series의 값을 DataFrame의 새 열로 추가하는 가장 좋은 방법은 다음을 사용할 수 있습니다 assign.

df1 = df1.assign(e=pd.Series(np.random.randn(sLength)).values)

다음은 새 열을 추가하는 간단한 방법입니다. df['e'] = e


기존 데이터 프레임에 새 열 'e'를 추가하고 데이터 프레임에서 아무것도 변경하지 않고 싶습니다. (시리즈는 항상 데이터 프레임과 길이가 같습니다.)

의 인덱스 값 edf1.

이라는 새 열을 시작하고 여기에 e시리즈의 값을 할당 하는 가장 쉬운 방법은 다음 e같습니다.

df['e'] = e.values

할당 (팬더 0.16.0 이상)

Pandas 0.16.0부터는 DataFrame에 assign새 열을 할당하고 새 열 외에 모든 원본 열이있는 새 개체 (사본)를 반환하는를 사용할 수도 있습니다.

df1 = df1.assign(e=e.values)

이 예 (도의 소스 코드를 포함하는 assign기능)은 또한 하나 개 이상의 열을 포함 할 수있다 :

df = pd.DataFrame({'a': [1, 2], 'b': [3, 4]})
>>> df.assign(mean_a=df.a.mean(), mean_b=df.b.mean())
   a  b  mean_a  mean_b
0  1  3     1.5     3.5
1  2  4     1.5     3.5

귀하의 예와 관련하여 :

np.random.seed(0)
df1 = pd.DataFrame(np.random.randn(10, 4), columns=['a', 'b', 'c', 'd'])
mask = df1.applymap(lambda x: x <-0.7)
df1 = df1[-mask.any(axis=1)]
sLength = len(df1['a'])
e = pd.Series(np.random.randn(sLength))

>>> df1
          a         b         c         d
0  1.764052  0.400157  0.978738  2.240893
2 -0.103219  0.410599  0.144044  1.454274
3  0.761038  0.121675  0.443863  0.333674
7  1.532779  1.469359  0.154947  0.378163
9  1.230291  1.202380 -0.387327 -0.302303

>>> e
0   -1.048553
1   -1.420018
2   -1.706270
3    1.950775
4   -0.509652
dtype: float64

df1 = df1.assign(e=e.values)

>>> df1
          a         b         c         d         e
0  1.764052  0.400157  0.978738  2.240893 -1.048553
2 -0.103219  0.410599  0.144044  1.454274 -1.420018
3  0.761038  0.121675  0.443863  0.333674 -1.706270
7  1.532779  1.469359  0.154947  0.378163  1.950775
9  1.230291  1.202380 -0.387327 -0.302303 -0.509652

이 새로운 기능이 처음 도입되었을 때의 설명은 여기 에서 찾을 수 있습니다 .


NumPy통해 직접 수행하는 것이 가장 효율적입니다.

df1['e'] = np.random.randn(sLength)

내 원래의 (매우 오래된) 제안은 map(훨씬 느리게) 사용 하는 것이 었습니다 .

df1['e'] = df1['a'].map(lambda x: np.random.random())

최신 Pandas 버전에서는 df.assign 을 사용하는 같습니다.

df1 = df1.assign(e=np.random.randn(sLength))

그것은 생성하지 않습니다 SettingWithCopyWarning.


매우 간단한 열 할당

Pandas 데이터 프레임은 정렬 된 열 사전으로 구현됩니다.

__getitem__ [], 특정 열을 가져 오는 __setitem__ [] =데 사용할 수있을 뿐만 아니라 새 열을 할당하는 데 사용할 수도 있습니다.

예를 들어,이 데이터 프레임은 단순히 []접근 자를 사용하여 열을 추가 할 수 있습니다.

    size      name color
0    big      rose   red
1  small    violet  blue
2  small     tulip   red
3  small  harebell  blue

df['protected'] = ['no', 'no', 'no', 'yes']

    size      name color protected
0    big      rose   red        no
1  small    violet  blue        no
2  small     tulip   red        no
3  small  harebell  blue       yes

이것은 데이터 프레임의 인덱스가 꺼져 있어도 작동합니다.

df.index = [3,2,1,0]
df['protected'] = ['no', 'no', 'no', 'yes']
    size      name color protected
3    big      rose   red        no
2  small    violet  blue        no
1  small     tulip   red        no
0  small  harebell  blue       yes

[] =은 갈 길이지만 조심하세요!

그러나 pd.Seriesa가 있고 인덱스가 꺼진 데이터 프레임에 할당하려고하면 문제가 발생합니다. 예보기 :

df['protected'] = pd.Series(['no', 'no', 'no', 'yes'])
    size      name color protected
3    big      rose   red       yes
2  small    violet  blue        no
1  small     tulip   red        no
0  small  harebell  blue        no

pd.Series기본적으로 a 에는 0에서 n까지 열거 된 인덱스 가 있기 때문 입니다. 그리고 pandas [] =방법 은 "스마트" 하려고 합니다.

실제로 무슨 일이 일어나고 있는지.

[] =방법 을 사용할 때 pandas는 왼손 데이터 프레임의 인덱스와 오른손 시리즈의 인덱스를 사용하여 외부 조인 또는 외부 병합을 조용히 수행합니다.df['column'] = series

사이드 노트

[]=방법은 입력에 따라 많은 다른 일을 시도하기 때문에 빠르게인지 부조화를 유발 하고 팬더의 작동 방식을 알지 않으면 결과를 예측할 수 없습니다 . 따라서 나는 []=코드 기반 에 대해 조언을 구하지 만 노트북에서 데이터를 탐색 할 때는 괜찮습니다.

문제 해결

a가 pd.Series있고 위에서 아래로 할당되기를 원하거나 생산적인 코드를 코딩하고 있고 인덱스 순서가 확실하지 않은 경우 이러한 종류의 문제를 보호하는 것이 좋습니다.

pd.Series를 a np.ndarray또는 a로 다운 캐스트 할 수 list있습니다.

df['protected'] = pd.Series(['no', 'no', 'no', 'yes']).values

또는

df['protected'] = list(pd.Series(['no', 'no', 'no', 'yes']))

그러나 이것은 그다지 명시 적이 지 않습니다.

일부 코더는 "이거 중복 된 것 같네요. 최적화하겠습니다"라고 말할 수 있습니다.

명시적인 방법

의 인덱스 설정 pd.Series의 인덱스가있을 수 있습니다 df명시 적입니다.

df['protected'] = pd.Series(['no', 'no', 'no', 'yes'], index=df.index)

또는 더 현실적으로 pd.Series이미 사용 가능할 것입니다.

protected_series = pd.Series(['no', 'no', 'no', 'yes'])
protected_series.index = df.index

3     no
2     no
1     no
0    yes

이제 할당 가능

df['protected'] = protected_series

    size      name color protected
3    big      rose   red        no
2  small    violet  blue        no
1  small     tulip   red        no
0  small  harebell  blue       yes

대체 방법 df.reset_index()

인덱스 불협화음 문제이기 때문에 당신이 dataframe의 인덱스가 있다고 판단되는 경우, 해야 일을 지시하지, 당신은 단순히이 빨리해야한다, 인덱스를 삭제할 수 있습니다,하지만 기능은 이제부터 그것은 매우 깨끗하지 않다 아마 두 가지 작업을 수행합니다.

df.reset_index(drop=True)
protected_series.reset_index(drop=True)
df['protected'] = protected_series

    size      name color protected
0    big      rose   red        no
1  small    violet  blue        no
2  small     tulip   red        no
3  small  harebell  blue       yes

참고 사항 df.assign

df.assign당신이하고있는 일을 더 명확하게 하지만 실제로는 위와 같은 문제가 있습니다.[]=

df.assign(protected=pd.Series(['no', 'no', 'no', 'yes']))
    size      name color protected
3    big      rose   red       yes
2  small    violet  blue        no
1  small     tulip   red        no
0  small  harebell  blue        no

그냥 함께 조심 df.assign당신의 열이 호출되지 self. 오류가 발생합니다. 함수에 이런 종류의 인공물 이 있기 때문에 df.assign 냄새 가납니다.

df.assign(self=pd.Series(['no', 'no', 'no', 'yes'])
TypeError: assign() got multiple values for keyword argument 'self'

당신은 "글쎄, 나는 self그때 사용하지 않을 것이다"라고 말할 수 있습니다 . 그러나 새로운 주장을 지원하기 위해 미래에이 함수가 어떻게 변경되는지 누가 압니까? 아마도 열 이름이 pandas의 새 업데이트에서 인수가되어 업그레이드에 문제를 일으킬 수 있습니다.


전체 새 열을 초기 기본 값 (예 :)으로 설정하려면 None다음을 수행 할 수 있습니다.df1['e'] = None

이것은 실제로 "객체"유형을 셀에 할당합니다. 따라서 나중에 목록과 같은 복잡한 데이터 유형을 개별 셀에 자유롭게 넣을 수 있습니다.


가장 쉬운 방법 :-

data['new_col'] = list_of_values

data.loc[ : , 'new_col'] = list_of_values

나는 SettingWithCopyWarning두려웠 고 iloc 구문을 사용하여 수정되지 않았습니다. My DataFrame은 ODBC 소스에서 read_sql에 의해 생성되었습니다. 위의 lowtech의 제안을 사용하여 다음이 저에게 효과적이었습니다.

df.insert(len(df.columns), 'e', pd.Series(np.random.randn(sLength),  index=df.index))

이것은 끝에 열을 삽입하는 데 잘 작동했습니다. 가장 효율적인지 모르겠지만 경고 메시지가 마음에 들지 않습니다. 더 나은 해결책이 있다고 생각하지만 찾을 수 없으며 색인의 일부 측면에 달려 있다고 생각합니다.
참고 . 이것은 한 번만 작동하며 기존 열을 덮어 쓰려고하면 오류 메시지가 표시됩니다.
참고 위와 같이 0.16.0부터 할당이 가장 좋은 솔루션입니다. http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.assign.html#pandas.DataFrame.assign 문서를 참조 하세요. 중간 값을 덮어 쓰지 않는 데이터 흐름 유형에 적합합니다.


  1. 먼저 list_of_e관련 데이터가 있는 파이썬을 만듭니다 .
  2. 이것을 사용하십시오 : df['e'] = list_of_e

추가하려는 열이 계열 변수이면 다음과 같습니다.

df["new_columns_name"]=series_variable_name #this will do it for you

이것은 기존 컬럼을 교체하는 경우에도 잘 작동합니다. 교체하려는 컬럼과 동일한 new_columns_name을 입력하기 만하면 기존 컬럼 데이터를 새 시리즈 데이터로 덮어 씁니다.


완벽한 보안 :

df.loc[:, 'NewCol'] = 'New_Val'

예:

df = pd.DataFrame(data=np.random.randn(20, 4), columns=['A', 'B', 'C', 'D'])

df

           A         B         C         D
0  -0.761269  0.477348  1.170614  0.752714
1   1.217250 -0.930860 -0.769324 -0.408642
2  -0.619679 -1.227659 -0.259135  1.700294
3  -0.147354  0.778707  0.479145  2.284143
4  -0.529529  0.000571  0.913779  1.395894
5   2.592400  0.637253  1.441096 -0.631468
6   0.757178  0.240012 -0.553820  1.177202
7  -0.986128 -1.313843  0.788589 -0.707836
8   0.606985 -2.232903 -1.358107 -2.855494
9  -0.692013  0.671866  1.179466 -1.180351
10 -1.093707 -0.530600  0.182926 -1.296494
11 -0.143273 -0.503199 -1.328728  0.610552
12 -0.923110 -1.365890 -1.366202 -1.185999
13 -2.026832  0.273593 -0.440426 -0.627423
14 -0.054503 -0.788866 -0.228088 -0.404783
15  0.955298 -1.430019  1.434071 -0.088215
16 -0.227946  0.047462  0.373573 -0.111675
17  1.627912  0.043611  1.743403 -0.012714
18  0.693458  0.144327  0.329500 -0.655045
19  0.104425  0.037412  0.450598 -0.923387


df.drop([3, 5, 8, 10, 18], inplace=True)

df

           A         B         C         D
0  -0.761269  0.477348  1.170614  0.752714
1   1.217250 -0.930860 -0.769324 -0.408642
2  -0.619679 -1.227659 -0.259135  1.700294
4  -0.529529  0.000571  0.913779  1.395894
6   0.757178  0.240012 -0.553820  1.177202
7  -0.986128 -1.313843  0.788589 -0.707836
9  -0.692013  0.671866  1.179466 -1.180351
11 -0.143273 -0.503199 -1.328728  0.610552
12 -0.923110 -1.365890 -1.366202 -1.185999
13 -2.026832  0.273593 -0.440426 -0.627423
14 -0.054503 -0.788866 -0.228088 -0.404783
15  0.955298 -1.430019  1.434071 -0.088215
16 -0.227946  0.047462  0.373573 -0.111675
17  1.627912  0.043611  1.743403 -0.012714
19  0.104425  0.037412  0.450598 -0.923387

df.loc[:, 'NewCol'] = 0

df
           A         B         C         D  NewCol
0  -0.761269  0.477348  1.170614  0.752714       0
1   1.217250 -0.930860 -0.769324 -0.408642       0
2  -0.619679 -1.227659 -0.259135  1.700294       0
4  -0.529529  0.000571  0.913779  1.395894       0
6   0.757178  0.240012 -0.553820  1.177202       0
7  -0.986128 -1.313843  0.788589 -0.707836       0
9  -0.692013  0.671866  1.179466 -1.180351       0
11 -0.143273 -0.503199 -1.328728  0.610552       0
12 -0.923110 -1.365890 -1.366202 -1.185999       0
13 -2.026832  0.273593 -0.440426 -0.627423       0
14 -0.054503 -0.788866 -0.228088 -0.404783       0
15  0.955298 -1.430019  1.434071 -0.088215       0
16 -0.227946  0.047462  0.373573 -0.111675       0
17  1.627912  0.043611  1.743403 -0.012714       0
19  0.104425  0.037412  0.450598 -0.923387       0

데이터 프레임 및 시리즈 개체가있는 경우 같은 인덱스를 , pandas.concat또한 여기에서 작동합니다 :

import pandas as pd
df
#          a            b           c           d
#0  0.671399     0.101208   -0.181532    0.241273
#1  0.446172    -0.243316    0.051767    1.577318
#2  0.614758     0.075793   -0.451460   -0.012493

e = pd.Series([-0.335485, -1.166658, -0.385571])    
e
#0   -0.335485
#1   -1.166658
#2   -0.385571
#dtype: float64

# here we need to give the series object a name which converts to the new  column name 
# in the result
df = pd.concat([df, e.rename("e")], axis=1)
df

#          a            b           c           d           e
#0  0.671399     0.101208   -0.181532    0.241273   -0.335485
#1  0.446172    -0.243316    0.051767    1.577318   -1.166658
#2  0.614758     0.075793   -0.451460   -0.012493   -0.385571

동일한 색인이없는 경우 :

e.index = df.index
df = pd.concat([df, e.rename("e")], axis=1)

새 열을 할당하기 전에 인덱싱 된 데이터가있는 경우 인덱스를 정렬해야합니다. 적어도 내 경우에는 다음을 수행해야했습니다.

data.set_index(['index_column'], inplace=True)
"if index is unsorted, assignment of a new column will fail"        
data.sort_index(inplace = True)
data.loc['index_value1', 'column_y'] = np.random.randn(data.loc['index_value1', 'column_x'].shape[0])

하지만 한 가지 주목할 점은

df1['e'] = Series(np.random.randn(sLength), index=df1.index)

이것은 사실상 df1.index 왼쪽 조인 이 될 것 입니다. 따라서 외부 조인 효과 를 원할 경우 아마도 불완전한 해결책은 데이터 유니버스를 포괄하는 인덱스 값으로 데이터 프레임을 만든 다음 위의 코드를 사용하는 것입니다. 예를 들면

data = pd.DataFrame(index=all_possible_values)
df1['e'] = Series(np.random.randn(sLength), index=df1.index)

Let me just add that, just like for hum3, .loc didn't solve the SettingWithCopyWarning and I had to resort to df.insert(). In my case false positive was generated by "fake" chain indexing dict['a']['e'], where 'e' is the new column, and dict['a'] is a DataFrame coming from dictionary.

Also note that if you know what you are doing, you can switch of the warning using pd.options.mode.chained_assignment = None and than use one of the other solutions given here.


I was looking for a general way of adding a column of numpy.nans to a dataframe without getting the dumb SettingWithCopyWarning.

From the following:

  • the answers here
  • this question about passing a variable as a keyword argument
  • this method for generating a numpy array of NaNs in-line

I came up with this:

col = 'column_name'
df = df.assign(**{col:numpy.full(len(df), numpy.nan)})

To add a new column, 'e', to the existing data frame

 df1.loc[:,'e'] = Series(np.random.randn(sLength))

For the sake of completeness - yet another solution using DataFrame.eval() method:

Data:

In [44]: e
Out[44]:
0    1.225506
1   -1.033944
2   -0.498953
3   -0.373332
4    0.615030
5   -0.622436
dtype: float64

In [45]: df1
Out[45]:
          a         b         c         d
0 -0.634222 -0.103264  0.745069  0.801288
4  0.782387 -0.090279  0.757662 -0.602408
5 -0.117456  2.124496  1.057301  0.765466
7  0.767532  0.104304 -0.586850  1.051297
8 -0.103272  0.958334  1.163092  1.182315
9 -0.616254  0.296678 -0.112027  0.679112

Solution:

In [46]: df1.eval("e = @e.values", inplace=True)

In [47]: df1
Out[47]:
          a         b         c         d         e
0 -0.634222 -0.103264  0.745069  0.801288  1.225506
4  0.782387 -0.090279  0.757662 -0.602408 -1.033944
5 -0.117456  2.124496  1.057301  0.765466 -0.498953
7  0.767532  0.104304 -0.586850  1.051297 -0.373332
8 -0.103272  0.958334  1.163092  1.182315  0.615030
9 -0.616254  0.296678 -0.112027  0.679112 -0.622436

The following is what I did... But I'm pretty new to pandas and really Python in general, so no promises.

df = pd.DataFrame([[1, 2], [3, 4], [5,6]], columns=list('AB'))

newCol = [3,5,7]
newName = 'C'

values = np.insert(df.values,df.shape[1],newCol,axis=1)
header = df.columns.values.tolist()
header.append(newName)

df = pd.DataFrame(values,columns=header)

If you get the SettingWithCopyWarning, an easy fix is to copy the DataFrame you are trying to add a column to.

df = df.copy()
df['col_name'] = values

to insert a new column at a given location (0 <= loc <= amount of columns) in a data frame, just use Dataframe.insert:

DataFrame.insert(loc, column, value)

Therefore, if you want to add the column e at the end of a data frame called df, you can use:

e = [-0.335485, -1.166658, -0.385571]    
DataFrame.insert(loc=len(df.columns), column='e', value=e)

value can be a Series, an integer (in which case all cells get filled with this one value), or an array-like structure

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.insert.html

참고URL : https://stackoverflow.com/questions/12555323/adding-new-column-to-existing-dataframe-in-python-pandas

반응형