我正在尝试根据组ID逐行填充NaN值。
我尝试使用fillNA,使用正向和反向填充选项,但是fillNA函数不会逐行填充数据帧。此外,我想确保在填写NaN值之前,公司匹配。在这种情况下,使用前向填充将使公司“ Pear”被公司“ Banana”的数据填充。
appended = appended.sort_values(by=['Company','Intro'],na_position='last')
appended = appended.reset_index(drop=True)
for i in appended.index:
if i==0:
pass
else:
if appended.at[i,'Company']==appended.at[i-1,'Company']:
appended.fillna(method='ffill',inplace=True)
else:
pass
附加数据框
Company Intro Categories Headquarters Founded Date Funding Stage
Apple xyz Healthcare, Big Data New York 2018 Series A
Apple NaN NaN NaN NaN NaN
Apple NaN NaN NaN NaN NaN
Banana Lier Government Europe 2010 Series B
Pear NaN NaN NaN NaN NaN
这是我希望达到的预期结果:
Expected Result
Company Intro Categories Headquarters Founded Date Funding Stage
Apple xyz Healthcare, Big Data New York 2018 Series A
Apple xyz Healthcare, Big Data New York 2018 Series A
Apple xyz Healthcare, Big Data New York 2018 Series A
Banana Lier Government Europe 2010 Series B
Pear NaN NaN NaN NaN NaN
答案 0 :(得分:2)
df.groupby(['Company']).ffill()
Company Intro Categories Headquarters Founded Date Funding Stage
0 Apple xyz Healthcare, Big Data New York 2018.0 Series A
1 Apple xyz Healthcare, Big Data New York 2018.0 Series A
2 Apple xyz Healthcare, Big Data New York 2018.0 Series A
3 Banana Lier Government Europe 2010.0 Series B
4 Pear NaN NaN NaN NaN NaN
答案 1 :(得分:0)
import pandas as pd
from io import StringIO
# sample data
df = pd.read_fwf(StringIO("""
Company Intro Categories Headquarters Founded_Date Funding_Stage
Apple xyz Healthcare, Big Data New York 2018 Series A
Apple NaN NaN NaN NaN NaN
Apple NaN NaN NaN NaN NaN
Banana Lier Government Europe 2010 Series B
Pear NaN NaN NaN NaN NaN"""), header=1)
# Create the summary level - assumes repeat data comes first
df_summary = df.groupby("Company").head(1)
# Join the result
df_result = df[['Company']].merge(df_summary, on="Company")
# Company Intro Categories Headquarters Founded_Date Funding_Stage
#0 Apple xyz Healthcare, Big Data New York 2018.0 Series A
#1 Apple xyz Healthcare, Big Data New York 2018.0 Series A
#2 Apple xyz Healthcare, Big Data New York 2018.0 Series A
#3 Banana Lier Government Europe 2010.0 Series B
#4 Pear NaN NaN NaN NaN NaN