简体   繁体   English

Python 重新格式化 Dataframe

[英]Python Reformat Dataframe

I am trying to iterate through the dataframe and if the row's value 'Age' column is empty, it will move the value in 'Company' column to the 'Location' column of the previous row.我正在尝试遍历 dataframe 并且如果该行的值“年龄”列为空,它会将“公司”列中的值移动到前一行的“位置”列。 Is there a quick way to do this?有没有快速的方法来做到这一点?

As-Is原样

As-Is Dataframe原样 Dataframe

To-Be成为

To-Be Dataframe准Dataframe

Here is a solution that works by iterating over the rows and copying the row's Name value to the previous row's Location value.这是一个解决方案,它通过迭代行并将行的Name值复制到前一行的Location值。 Note this will break if the first row's Age is empty.请注意,如果第一行的Age为空,这将中断。

I'm pretty sure there's a more efficient way of dropping the empty rows at the end, so I welcome edits!我很确定有一种更有效的方法可以在最后删除空行,所以我欢迎编辑!

d = {'Name': ["Amber", "North", "Max", "South", "Jackson", "East"], 'Age': ["21", "", "23", "", "38", ""], "Location":["","","","","",""]}
df = pandas.DataFrame(data=d)

rows_to_drop = []

for index, values in df.iterrows():

    if not values["Age"]:

        df["Location"][index-1] = df["Name"][index]
        rows_to_drop.append(index)

df = df.drop(rows_to_drop)

You can use numpy :您可以使用numpy

arr = df.to_numpy()
arr[::2, -1] = arr[1::2,0]
df = pd.DataFrame(arr[::2], columns=df.columns)

Output: Output:

      Name Age Location
0    Amber  21    North
1      Max  23    South
2  Jackson  38     East

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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