def test(xx):
return xx + 1
df = pd.DataFrame({'values':[i for i in range(20)]})
df_filtered = df.iloc[6:15, :]
# For slicing rows explicitly, from 6 up to but not including 15
df_filtered = df_filtered.apply(test, axis=1)
df_filtered = df_filtered.rename(columns={'values':'new_values'})
df = df.join(df_filtered) # joins on original index
IIUC:
^{} 允许您根据行(和列)的位置选择行(和列)
您可以为数据帧创建只有特定行的筛选副本,然后在该筛选副本上运行apply或lambda。然后在原始索引上合并以获得输出。您需要决定如何处理行的空值,您没有做任何事情来覆盖原始列吗?请注意,apply和lambda的速度很慢,所以如果您可以避免它,您会过得更好。过滤df可以减少lambda需要做的工作量
相关问题 更多 >
编程相关推荐