0

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!

Murray Ross
  • 163
  • 9
  • Please provide a complete reproducible minimal example (input+output): [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – mozway May 04 '22 at 07:52
  • I have added my input, and shown the desired output and the current state of the output. – Murray Ross May 04 '22 at 08:01

2 Answers2

1

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

Whereismywall
  • 188
  • 1
  • 12
0

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

Whereismywall
  • 188
  • 1
  • 12
  • Thanks, but that doesn't add the values from `raw_data['CycleNumber']` after, as that's part of my desired output? – Murray Ross May 04 '22 at 08:10
  • Then use ```interim_data_output['CN5']=raw_data['CycleNumber']``` instead of the first line ? – D.Manasreh May 04 '22 at 08:12
  • @MurrayRoss use the solution mentioned above – Whereismywall May 04 '22 at 08:13
  • Thanks, but that replaces the first value of `raw_data['CycleNumber']` when `interim_data_output['CN5'].iloc[0]='*Cycle number'` is performed? – Murray Ross May 04 '22 at 08:26
  • `interim_data_output['CN5']=raw_data['CycleNumber'] interim_data_output['CN5'].iloc[0]='*Cycle number'` – Whereismywall May 04 '22 at 08:33
  • Yes, but that replaces the first value of `raw_data['CycleNumber']` when you insert the `'*Cycle Number' ` at position 0 of the `interim_data_output`. I don't want to replace any values, I want to retain them all and merely place the `'*Cycle number'` before them. – Murray Ross May 04 '22 at 08:38
  • i am not able understand what you mean, do you want to append the text '*Cycle number' before each value ? like `0 *Cycle number 1*Cycle number ` – Whereismywall May 04 '22 at 08:41
  • No, I just want to append it before the first value, but the code provided replaces the first value, it does not append it before all the other values. So if my `raw_data` had 50,000 values, then `interim_data_output` would have 50,001 - the value `*Cycle number` and all the values from the raw_data column. – Murray Ross May 04 '22 at 08:44