Set value to an entire column of a pandas dataframe

前端 未结 8 2050
甜味超标
甜味超标 2020-12-13 05:54

I\'m trying to set the entire column of a dataframe to a specific value.

In  [1]: df
Out [1]: 
     issueid   industry
0        001        xxx
1        002           


        
8条回答
  •  天涯浪人
    2020-12-13 06:37

    Assuming your Data frame is like 'Data' you have to consider if your data is a string or an integer. Both are treated differently. So in this case you need be specific about that.

    import pandas as pd
    
    data = [('001','xxx'), ('002','xxx'), ('003','xxx'), ('004','xxx'), ('005','xxx')]
    
    df = pd.DataFrame(data,columns=['issueid', 'industry'])
    
    print("Old DataFrame")
    print(df)
    
    df.loc[:,'industry'] = str('yyy')
    
    print("New DataFrame")
    print(df)
    

    Now if want to put numbers instead of letters you must create and array

    list_of_ones = [1,1,1,1,1]
    df.loc[:,'industry'] = list_of_ones
    print(df)
    

    Or if you are using Numpy

    import numpy as np
    n = len(df)
    df.loc[:,'industry'] = np.ones(n)
    print(df)
    

提交回复
热议问题