简体   繁体   English

Pandas:将值从一列转移到另一列,并使用 python 删除重复项

[英]Pandas: shift values from one column to other, and drop duplicates using python

Suppose I am getting a dataframe like this:假设我得到一个像这样的 dataframe:

Name            value
Umicore         470
889 
19  
912 
1.68    
    
Shopify         19
500 
17  
51  
1.44    

How do get a dataframe such that I will be left with this output如何获得 dataframe 以便我将留下这个 output

Name            value
Umicore         1.68    
Shopify         1.44    

This is how I am getting my dataframe:这就是我得到 dataframe 的方式:

#my_df['Name'].replace('', np.nan, inplace=True)
#my_df['Name'].replace('', np.nan).ffill(inplace=True) #tried just now fails
#my_df['value'].replace('', np.nan, inplace=True)
#my_df.dropna(subset=['Name', 'value'], inplace=True)
my_df.drop_duplicates(keep='last', inplace=True)
my_df.to_csv('output.csv', index=False)

How do I shift the numbers from Name column to Value column?如何将数字从名称列转移到值列? Please help!请帮忙!

Use:利用:

#if empty strings or NaNs in Name column remove these rows
df['Name'] = df['Name'].replace('', np.nan)
df = df.dropna(subset=['Name'])

#create default index
df = df.reset_index(drop=True)    

print (df)
      Name  value
0  Umicore  470.0
1      889    NaN
2       19    NaN
3      912    NaN
4     1.68    NaN
5  Shopify   19.0
6      500    NaN
7       17    NaN
8       51    NaN
9     1.44    NaN

#convert values to numeric, if no numeric is NaN created
s = pd.to_numeric(df['Name'], errors='coerce')
#forward filling missing values by non numeric values
df['Name'] = df['Name'].where(s.isna()).ffill()
#set value by s
df['value'] = s

print (df)
      Name   value
0  Umicore     NaN
1  Umicore  889.00
2  Umicore   19.00
3  Umicore  912.00
4  Umicore    1.68 <- last value of Umicore
5  Shopify     NaN
6  Shopify  500.00
7  Shopify   17.00
8  Shopify   51.00
9  Shopify    1.44

#remove duplicates by Name column
df = df.drop_duplicates('Name',keep='last')
print (df)
      Name  value
4  Umicore   1.68
9  Shopify   1.44

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

相关问题 如何根据 DataFrame Python Pandas 中其他 2 列中的值删除一列中的重复项? - How to drop duplicates in one column based on values in 2 other columns in DataFrame in Python Pandas? 根据另一列的重复项删除一列的重复项,将另一列重复项保留在 pandas - drop duplicates of one column based on duplicates of another column keeping the other column duplicates in pandas 根据另一列(Python,Pandas)中的值删除一列的重复项 - Drop duplicates of one column based on value in another column, Python, Pandas Python Pandas:多列数据框-移一列而不更改另一列 - Python pandas: Multicolumn dataframe - Shift one column without changing the other 熊猫:按其他列值移动一列 - Pandas: Shift one column by other column value 根据其他列值从熊猫 dataframe 中删除重复项 - Drop duplicates from a panda dataframe based on other column values 将值从另一列python pandas转移到另一列 - shift values from another column python pandas to another column 如何在 python / pandas 中将一些值从一列转移到另一列? [等候接听] - How to shift some values from one column to another in python / pandas? [on hold] Python Pandas:从列中删除正在进行的值 - Python Pandas: Drop ongoing values from column 大熊猫:按列值删除准重复项 - Pandas: Drop quasi-duplicates by column values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM