Home > Software engineering >  Creating a new pandas dataframe column with one added value, then a copy of values from another colu
Creating a new pandas dataframe column with one added value, then a copy of values from another colu

Time:05-05

I'm currently trying to construct a new pandas dataframe, interim_data_output to copy raw data values into, in order to perform calculations on. My idea to do this is to have a subheading, Cycle number, and then all the values from another column in another dataframe, column raw_data['CycleNumber'] after it.

My input column, raw_data['CycleNumber'] looks like this:

print(raw_data['CycleNumber'])

0    1
1    1
2    1
3    1
4    1
5    1
6    1
7    1
Name: CycleNumber, dtype: int64

So I would start a new column, interim_data_output['CN5'] , with an additional value at the start of the column, *Cycle number', and then all the values from the raw data column raw_data['CycleNumber'] following after this extra value.

My So my intended output would be:

print (interim_data_output['CN5'])

     CN5
0    *Cycle number
1    1
2    1
3    1

My original idea to get the desired output was to try this:

interim_data_output = pd.DataFrame()
interim_data_output['CN5'] = '*Cycle number', raw_data['CycleNumber']

This however doesn't work, as I merely get this:

print (interim_data_output['CN5'])

0                                        *Cycle number
1    0           1
1           1
2           1
3   ...
Name: CN5, dtype: object

I realise this is something very easy probably, but what am I missing here? Any help would be appreciated!

CodePudding user response:

k=raw_data['CycleNumber'].tolist() k.insert(0, '*Cycle number') data_new={'CN5' : k} interim_data_output=pd.DataFrame(data_new) Please ignore the formatting. this should work

CodePudding user response:

interim_data_output['CN5']=1
interim_data_output['CN5'].iloc[0]='*Cycle number'

  • Related