python uses pandas to assign values to multiple columns at the same time

  • 2021-09-24 23:09:52
  • OfStack

Such as dataframe


 data1[' Month ']=int(month) # Add month and business name 
 data1[' Enterprise ']=parmentname

You can add a single column and assign values. If you want to assign values to multiple columns at the same time,


data1[' Month ',' Enterprise ']=int(month) , parmentname   # Add month and business name 

Will make mistakes

ValueError: Length of values does not match length of index


data[[' Total ',' Average ']]=' Data ',' Month '

Something like this is also invalid

KeyError: "None of [Index (['Total', 'Average'], dtype = 'object')] are in the [columns]"

Only in the following examples:


import pandas as pd
chengji=[[100,95,100,99],[90,98,99,100],[88,95,98,88],[99,98,97,87],[96.5,90,96,85],[94,94,93,91],[91, 99, 92, 87], [85, 88, 85, 90], [90, 92, 99, 88], [90, 88, 89, 81], [85, 89, 89, 82], [95, 87, 86, 88], [90, 97, 97, 98], [80, 92, 89, 98], [80, 98, 85, 81], [98, 88, 95, 92]]
data=pd.DataFrame(chengji,columns=[' Language ',' English ',' Mathematics ',' Politics '])
print (data)
# data1=data[[' Mathematics ',' Language ',' English ',' Politics ']]    # Sort 
# data1=data1.reset_index(drop=True)   # Sequence reconstruction 
# data1.index.names=[' Serial number ']     # Sequence renaming 
# data1.index=data1.index+1    # Sequence from 1 Begin 
# print (data1)
data=pd.DataFrame(chengji,columns=[' Language ',' English ',' Mathematics ',' Politics '],index=[i for i in range(1,len(chengji)+1)])
print (data)
data[[' Total ',' Average ']]=data.apply(lambda x: (x.sum(), x.sum()/4),axis=1,result_type='expand')
print (data[:])
data=pd.DataFrame(chengji,columns=[' Language ',' English ',' Mathematics ',' Politics '],index=[i for i in range(1,len(chengji)+1)])
print (data)
data[[' Total ',' Average ']]=data.apply(lambda x:(' Data ',' Month '),axis=1,result_type='expand')
print (data[:])

Apply apply and set the result_type= 'expand' parameter.

In the previous example, just use the following method


data1[[' Month ',' Enterprise ']]=data1.apply(lambda x:(int(month),parmentname),axis=1,result_type='expand')
  # data1[' Month ']=int(month)   # Add month and business name 
  # data1[' Enterprise ']=parmentname
  #print (data1)

Postscript:

If the 'Month' and 'Enterprise' columns exist, you can use the following. In the above example, you can directly create non-existing columns.


data1.lco[:,[' Month ',' Enterprise ']]=int(month),parmentname

Or


data1[[' Month ',' Enterprise ']]=int(month),parmentname

Today, I encountered another one that intercepted the string length from one column and wrote it to another column, also 1 and wrote it here:

Goods are not listed in the original table, so take the first 12 digits of the goods code.


totaldata = totaldata.reset_index(drop=False)
totaldata[' Goods '] = totaldata[' Item code '].apply(lambda x:x[:12])

Postscript: On May 17, 2020, I encountered the problem of adding two columns and assigning values


import numpy as np
import pandas as pd
from pandas import Series
 
chengji = [['N', 95, 0], ['N', 100, 88], ['N', 88, 100], ['N', 66, 0]]
data = pd.DataFrame(chengji, columns=['p', 'x', 'g'])
data[[' Serial number ',' Column name ']]=data[['p','x']] #pd.DataFrame(data[['p','x']])# .apply(lambda x : x )
print(data)

Add: apply of pandas returns multiple columns and assigns values

The code is as follows:


import pandas as pd
df_tmp = pd.DataFrame([
 {"a":"data1", "cnt":100},{"a":"data2", "cnt":200},
])
df_tmp
a cnt
data1 100
data2 200

Method 1: Use the parameter result_type of apply to process


data1[' Month ',' Enterprise ']=int(month) , parmentname   # Add month and business name 
0

Method 2: Use zip packaging to return results to process


data1[' Month ',' Enterprise ']=int(month) , parmentname   # Add month and business name 
1

Related articles: