简体   繁体   English

使用pandas在csv文件的同一行上填充下一列值的行中的空值

[英]Fill empty values from a row with the value of next column on the same row on csv file with pandas

I have this type of DataFrame 我有这种类型的DataFrame

name     surname       middle

Frank    Doe           NaN
John     Nan           Wood
Jack     Putt          Nan
Frank    Nan           Joyce

I want to move "middle" values on NaN same rows values on "surname" column. 我想在“姓氏”列上的NaN相同行值上移动“中间”值。 How can i do this? 我怎样才能做到这一点? I tried to use the fillna method but got no results. 我尝试使用fillna方法,但没有得到任何结果。 Here is my code: 这是我的代码:

import os
from pandas.io.parsers import read_csv


for csvFilename in os.listdir('.'):
   if not csvFilename.endswith('.csv'):
      continue
data=read_csv(csvFilename)
filtered_data["surname"].fillna(filtered_data["middle"].mean(),inplace=True)
filtered_data.to_csv('output.csv' , index=False)

Conditional column flipping 条件列翻转

Using pd.isnull() , columns can be rearranged conditionally. 使用pd.isnull() ,可以有条件地重新排列列。

import pandas as pd
from cStringIO import StringIO

# Create fake DataFrame... you can read this in however you like
df = pd.read_table(StringIO('''
name     surname       middle
Frank    Doe           NaN
John     NaN           Wood
Jack     Putt          NaN
Frank    NaN           Joyce'''), sep='\s+')

print 'Original DataFrame:'
print df
print

# Assign the middle name to any surname with a NaN
df.loc[pd.isnull(df['surname']), 'surname'] = df[pd.isnull(df['surname'])]['middle']

print 'Manipulated DataFrame:'
print df
print

Original DataFrame:
    name surname middle
0  Frank     Doe    NaN
1   John     NaN   Wood
2   Jack    Putt    NaN
3  Frank     NaN  Joyce

Manipulated DataFrame:
    name surname middle
0  Frank     Doe    NaN
1   John    Wood   Wood
2   Jack    Putt    NaN
3  Frank   Joyce  Joyce

I think there is an easier way to do that: 我认为有一种更简单的方法:

df['surname'] = df['middle'].combine_first(df['surname'])
print(df)

Output: 输出:

    name surname middle
0  Frank     Doe    NaN
1   John    Wood   Wood
2   Jack    Putt    NaN
3  Frank   Joyce  Joyce

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

相关问题 根据Pandas中第二列的条件,用另一行的同一列的值填充特定行的列中的值 - Fill values in a column of a particular row with the value of same column from another row based on a condition on second column in Pandas Python/Pandas:如果值为 NaN 或 0,则用同一行内下一列的值填充 - Python/Pandas: if value is NaN or 0 then fill with the value from the next column within the same row Pandas:同列不同行如何填写值 - Pandas:How to fill in value from the same column but different row Pandas:根据下一行的值替换组中的列值 - Pandas: Replace column values in a group by based on value from the next row 将 Pandas Dataframe 中的一行数据输入到 csv 文件中的下一个空行 - Enter row of data from Pandas Dataframe to next empty row in a csv file 熊猫:比较行值并修改下一列的行值 - Pandas: Comparing a row value and modify next column's row values Pandas 用行值填充列 - Pandas fill column with row value 使用 for 循环将一行(熊猫)与下一行进行比较,如果不同,则从列中获取值 - Compare a row (pandas) with the next row using for loop, and if not the same get a value from a column 如何将列值添加到熊猫中同一列的下一行 - how to add the column values to the next row of the same column in pandas 用熊猫数据框中另一列的相同值填充空值 - fill up empty values with same value of another column in pandas dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM