简体   繁体   中英

How do I transfer data from one cell to another in python (but more complex)?

I am currently working on a python project and I can't seem to figure something out.

I have a file with a bunch of data something similar to this chart:

图表

Is there a way I could move the variable data from 'number' 2 row into 'number' 1 row (the empty cell) in python without manually doing it? I have about 1000 different rows of people so it would take way too long to copy and paste.

For reference, the final result should be like:

图表

Also keeping in mind, I am filling data so some of these are already complete in the data frame, so shifting/copying everything up a cell would not work.

Thank you in advance!

Here is the other problem I run into, explained in the comments:

Chart

Example of bfill:

import pandas as pd

df = pd.read_csv('test_text.txt')

'''
test_text.txt contains:
person,number,variable
1,1,
1,2,2
1,3,3

'''

print(df)

df = df.fillna(method='bfill')

print(df)

Output (df before and after):

   person  number  variable
0       1       1       NaN
1       1       2       2.0
2       1       3       3.0

   person  number  variable
0       1       1       2.0
1       1       2       2.0
2       1       3       3.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM