我希望根据一个if条件自动更改熊猫缺少值列的名称,最好使用“string_name_number”。数字应该从一个开始到最后一个缺失值结束。我已经决定将循环设置如下,以从字符串中选择数据。
但是,来自缺失列的结果(df2)保持不变。-答辩人i、jakson、答辩人i、答辩人i、jane、答辩人i、mary、.
我期望看到以下结果(df2);-应答1,jakson,被告2,被告3,jane,被告4,mary,.
请协助。
import pandas as pd
df = pd.read_csv('232 responses.csv', sep=',',header=0, parse_dates=True,
index_col='Timestamp')
missing_rows_list = list(range(0, len (df)))
for i in missing_rows_list:
i = 1
df2 = [df['Name (optional)']\
.replace(np.nan, 'respondent {d[i]}'\
.format(d=missing_rows_list)) if pd.isnull(df['Name (optional)']) \
else df['Name (optional)'] == word in df['Name (optional)']]
i += 1发布于 2017-07-31 06:01:07
我认为这应该是一种更方便的方法:
df=pd.DataFrame({"a":["test1","test2","test3","test4",np.NAN],"b":["test5",np.NAN,"test7",np.NAN,"test9"]})
#Create the respondent + inex number format --> you can also save this in an extra df column if you like
a=["respondent"]*len(df.index)
b=list(df.index)
c=["{0}{1}".format(a_,b_)for a_,b_ in list(zip(a,b))]
#Replace the missing values
for i in df.columns:
mask = df[i].isnull()
df[i].mask(mask,c, inplace=True)
print(df)
a b
0 test1 test5
1 test2 response1
2 test3 test7
3 test4 response3
4 response4 test9https://stackoverflow.com/questions/45395061
复制相似问题